我有这个代码:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i(TAG, "The id of the selected note is " + id);
Intent editNote = new Intent(this, TaskEditActivity.class);
editNote.putExtra(TasksDBAdapter.KEY_ID, id);
startActivityForResult(editNote, EDIT_TASK_REQUEST);
}
这段代码从不同的 Activity 中检索额外的:
if (savedInstanceState != null) {
id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
}
Log.i(TAG, "Id of note = " + id);
在第一个代码 fragment 中,Logcat 表示:所选注释的 id 为 2
,但在第二个代码 fragment 中,Logcat 表示:注释的 ID = 0
。这里刚刚发生了什么?对于这个非常烦人的问题的任何解决方案。
最佳答案
我认为您混淆了 Activity
暂停时保存的状态以及通过 Intent
传递到 Activity
的数据.
你想要这样的东西:
Bundle extras = getIntent().getExtras();
id = extras.getLong(TasksDBAdapter.KEY_ID);
传递给 onCreate()
的 Bundle
是您使用 the onSaveInstanceState()
method 保存的 Bundle
并且不是您添加到 Intent
中的额外 Bundle
。
关于java - Intent 不提取额外的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3397995/