java - Intent 不提取额外的

标签 java android android-intent extras

我有这个代码:

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/

相关文章:

java - 将 base64 格式的图像发送到 Java 网络服务器,转换并保存

java - 我需要有关成功连接到带有 galaxy s3 的 zephyr HXM 蓝牙设备的建议

焦点上的Android EditText左图标颜色变化?

Android 模拟器每 3 秒卡住一次

Android - URI 方案 - 停止打开多个应用程序实例

Android UIAutomator/espresso : open browser on a specific URL

java - 数组实例变量与setter方法摄入无关吗?

java - 获取 w3c 文档的名称

java - Facebook4J OAuth 失败

Android - 在进入父 Activity 时保留先前添加的额外 Intent