- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
感谢这个答案Android Fragment lifecycle issue (NullPointerException on onActivityResult)当我在调用 startActivityForResult 后在我的 fragment 中收到 NPE 时,我设法重新创建了一个场景。所以我有
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
break;
从我的 fragment 调用,然后我的 Activity 接收到 onPause、onStop 和 onDestroy,因此调用 startActivityForResult
的 fragment 也获得了 onDestroy。在我选择一张图片后,我在我的 Activity 上获得了一个新的 onCreate,然后我在我现在被销毁的原始 fragment 上获得了一个 public void onActivityResult
。
我的问题是,由于这可能(尽管很少见)情况,如何恢复传递给它们的整个 fragment 和对象堆栈,以及如何防止原始 fragment 泄漏?
最佳答案
当您的代码执行以下行时:
startActivityForResult(photoPickerIntent, PHOTO_SELECT_REQUEST_CODE);
您的 Activity 每次都会得到 onPause()
、onStop()
而不是 onDestroy()
。如果 android 系统在那个运行时内存不足,那么你的 Activity 可能会得到 onDestroy()
为了保存 fragment 和 Activity 的状态并防止泄漏:
在 onSaveInstanceState(Bundle)
中保存您的状态
In situations where the system needs more memory it may kill paused processes (after
onPause()
) to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In generalonSaveInstanceState(Bundle)
is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)
在 onRestoreInstanceState(Bundle)
中恢复你的状态
onRestoreInstanceState(Bundle)
method is called afteronStart()
when the activity is being re-initialized from a previously saved state, given here insavedInstanceState
. Most implementations will simply useonCreate(Bundle)
to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen byonSaveInstanceState(Bundle)
.
这可以帮助您实现您的要求
关于Android Fragment 被销毁但收到 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29465182/
哪个更好,为什么? 这个: @Override public void onActivityResult(int requestCode, int resultCode, Intent intent)
在我的 fragment 中,我已经启动了用于照片捕获的 startActivityforresult Intent 。我已经覆盖了 fragment 类中的 onActivityResult 回调方
我已经实现了 Facebook 集成,并且在初始化部分工作正常,即使 Facebook APPID 也适用于应用程序。 让我更详细地解释一下:我已在 onCreate() 中添加并初始化 Facebo
我有代码可以让我从计算器中获取值并进一步使用它: //-----------------This section creates the keypad functionality for (int o
我陷入了一种情况,根据我的应用程序图表流程描述如下: 假设我在 Activity A 上,在单击按钮时,我从图库中选择图像,选择后,我将移动到 Activity B(它在内部来自 onActivity
我有一项 Activity 要求启用蓝牙和发现模式。 请求被正确执行并被 onactivityresult() 接收. 问题出在发现请求中。 如果我点击拒绝,那么 RESULT_CANCELED正确获
所以我尝试显示相机拍摄的图像,并希望稍后通过套接字发送它,但我注意到 onActivityResult() 方法没有被调用,现在导致了进一步的问题。我使用 RESULT_OK,但检查其值,使其不是负值
我的 MainActivity.java 中有一个类用作接口(interface): public class prova{ prova(){ } @JavascriptIn
我尝试通过启动 Intent 来使用 startActivityForResult 从照片库中进行选择并覆盖 onActivityResult 来获取图像。我不知道为什么,但是当我选择图像并尝试将其绘
每当我完成“ViewEvent” Activity 时,在“HomeFragment”中调用 onActivityResult() 时遇到问题。ViewEvents Activity 从适配器启动。
我有一个选择图像的 Intent (我在 fragment 中调用它): Intent intent = new Intent(Intent.ACTION_GET_CONTENT); int
我有一个 RecyclerView,其中的项目包含一个使用 startActivityForResult 启动 Activity 的按钮。 onClick 方法是在适配器内部实现的,并且在从新 Act
我有以下代码,它应该使用服务记录设备屏幕。 问题是,要使用它,我需要使用如下调用:startActivityForResult/onActivityResult,以获得能够录制屏幕的权限。 但是在 A
我有三项 Activity ,A、B 和 C(代码如下)。 A 使用 startActivityforResult 调用 B,并覆盖 onActivityResult,B 也使用 Activityfo
我有一个 Activity 1,它向 Activity 2 发送 Intent ,如下所示: Activity 1 periodDatetv.setOnClickListener(new OnClic
我正在尝试将其与我的应用程序集成:Capture Image from Camera and Display in Activity 当我打开 Activity 时,我完全使用了作者所写的内容,但出现
我有一个 MainActivity,它有两个 TextView。用户可以选择启动另一个 Activity 并从 ListView 中选择数据来填充这些 TextView。我的主要 Activity 有
这个问题在这里已经有了答案: How to manage startActivityForResult on Android (14 个回答) 3年前关闭。 请写下答案 如何在按钮单击时调用 frag
我怎么知道这个主题有很好的记录,我已经阅读了很多关于那个问题的文章,但我仍然遇到以下问题:当我用我的应用拍照并点击“验证”按钮时,没有任何反应。我正在做的事情的目的是:传递给 onActivityRe
从第一个 Activity 我想去第二个,然后从第二个到第三个。在第三个activity中,我想在EditText中输入姓名,然后按下按钮后,转到第一个activity,同时发送在第三项 Ac
我是一名优秀的程序员,十分优秀!