android - 在 startActivityForResult 中传递数据并在 onActivityResult 中取回

标签 android android-activity android-intent

我有一个带有上下文菜单的 ListView,用户可以在其中拍照。我是这样拍照的:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(intent, PICTURE_RESULT);

并像这样检索它:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICTURE_RESULT) {
        long itemId = ???
        savePicture(itemId, data.getExtras());
    }
}

如何传递 ListView 项目 ID?我尝试了 intent.putExtras 和 intent.getLongExtra,但数据未随结果 Intent 一起传递。我不能使用实例变量,因为如果用户在拍照时切换到风景, Activity 可能会被破坏。

最佳答案

将该变量存储为 Activity 类成员,然后您可以从该 Activity 类的每个地方使用它。 如果你想保存变量状态,那么你应该使用 onSaveInstance 状态回调。请参阅开发人员文档。

关于android - 在 startActivityForResult 中传递数据并在 onActivityResult 中取回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295539/

相关文章:

android - 在启动 Intent 之前,如何知道系统在android中选择了多少隐式 Intent

android - 如何在android中动态更新RecyclerView

android - 无法将 xml 数据解析为 xamarin android 上的 ListView

android - 显示一个 editText

android - 整个应用程序中的全局 "search function"

java - 为什么我的应用程序在我第二次打开时崩溃?

android - Android FEST无法针对IDE中的单元测试正确编译

android - 如何制作一个包含很多条目的详细表格?

android - 如何在不以 Intent 启动 Activity 的情况下从在 list 中注册的广播接收器调用 Activity 中的方法?

android - 使用 Intent 时不显示电子邮件正文