意图不提取额外的
我有这个代码:
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);
}
这个代码检索额外的 FROM A DIFFERENT ACTIVITY:
if (savedInstanceState != null) {
id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
}
Log.i(TAG, "Id of note = " + id);
在第一个代码片段中,Logcat 说:所选注释的 id 是2
,但在第二个代码片段中,Logcat 表示:Id of note = 0
。这里刚刚发生了什么?对于这个非常烦人的问题的任何解决方案。
I have this code:
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);
}
And this code that retrieves the extra FROM A DIFFERENT ACTIVITY:
if (savedInstanceState != null) {
id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
}
Log.i(TAG, "Id of note = " + id);
In the first code snippet, Logcat says: The id of the selected note is 2
, but in the second code snippet, Logcat says: Id of note = 0
. What just happened here? Any solutions to this VERY annoying problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您混淆了
Activity
暂停时保存的状态以及通过Intent
传递到Activity
的数据。您想要这样的东西:
传递给
onCreate()
的Bundle
是您使用onSaveInstanceState()
方法 并不是您添加到Intent
中的额外Bundle
。I think you're confusing the state which is saved when an
Activity
is paused and the data delivered to theActivity
via anIntent
.You want to have something like:
The
Bundle
passed toonCreate()
is theBundle
you saved with theonSaveInstanceState()
method and is not the extrasBundle
you added to yourIntent
.您正在以非常错误的方式检索额外的内容。将第二个代码段替换为:
以下是此代码中发生的情况:
getIntent()
返回您在第一个代码段中创建的Intent
(Intent
> 用于启动当前活动)。然后,.getLongExtra()
返回附加的额外信息。如果没有找到带有该标签和该数据类型(long)的额外信息,它将返回0。savedInstanceState
用于在内存不足的情况下被Android系统关闭时保存应用程序的状态。不要混淆这两者。You are retrieving the extra in a very wrong way. Replace your second code snippet with:
Here's what happens in this code:
getIntent()
returns theIntent
you created in your first code snippet (theIntent
that was used to launch the current activity). Then,.getLongExtra()
returns the attached extra information. If no extra information with that tag and that data type (long) is found, it will return 0.savedInstanceState
is used for saving your app's state when it is shut down by the Android system under low memory conditions. Don't confuse these two.