1

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

 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());
}

当我出去挑选一个联系人时,日志说在 outState 中存在正确的值,但 onCreate 日志 SavedInstanceState 为空。

我在这里没有做什么?这可能与 onActivityResult 的 Intent 有关吗?

编辑:呃!

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

那是开始一项新的活动,不会回到我现在的活动。

4

1 回答 1

3

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

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

但如果您使用的是片段,请使用:

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

在这里你可以阅读更多:保存和恢复

于 2013-09-06T13:42:28.370 回答