使用onSaveInstanceState问题

时间:2013-09-06 13:37:48

标签: android

到目前为止,这是我的代码。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        Log.d("FUApp", "Creating: " + savedInstanceState.toString());
    }
    else
    {
        Log.d("FUApp", "SavedInstanceState is null");
    }
    setContentView(R.layout.activity_invite_friends);

    inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    formHolder = (LinearLayout) findViewById(R.id.form_holder_layout);
    addNewRow();
    TelephonyManager tMgr =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String yourPhoneNumber = tMgr.getLine1Number();

    your_name = (TextView) findViewById(R.id.your_name);
    your_phone_number = (TextView) findViewById(R.id.your_phone_number);
    your_phone_number.setText(yourPhoneNumber);
}



public void addNewRow() {
    inflater.inflate(R.layout.form_invite_friend, formHolder, true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = new MenuInflater(this);
    menuInflater.inflate(R.menu.activity_invite_friend, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.invite_friend_menu:
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 1);

            addNewRow();
            break;
        default:
            super.onOptionsItemSelected(item);
    }
    return true;
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c =  managedQuery(contactData, null, null, null, null);
        if (c.moveToFirst()) {
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            Intent intent = new Intent(ActivityInviteFriends.this, ActivityInviteFriends.class);
            intent.putExtra("name", name);
            startActivityForResult(intent, 0);
        }
    }
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("your_name", String.valueOf(your_name.getText()));
    Log.d("FUApp", "onSaveInstanceState: " + outState.toString());
}

当我出去挑选一个联系人时,Log说在outState中有正确的值,但onCreate logs SavedInstanceState为null。

我在这里做的不是什么?是否可能与onActivityResult的Intent有关?

编辑:呃!

Intent intent = new Intent(ActivityInviteFriends.this, ActivityInviteFriends.class);
intent.putExtra("name", name);
startActivityForResult(intent, 0);

这开始了一项新的活动,不再回到现在的活动。

1 个答案:

答案 0 :(得分:3)

如果您使用活动,则应使用:

 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

但如果您使用片段使用:

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
}

在这里,您可以阅读更多内容:Save and Restore