上下文:
我有一个使用 Activities
和 Fragments
的应用。
我暂停它以访问另一个像谷歌翻译。在 GTranslate 中,我使用共享 Intent
过程通过 intent-filter
ACTION_SEND
将文本数据发送到我的应用程序。
即使第一个应用程序在后台,它的当前 activity
也会重新创建。
问题是与提到的 Activity
关联的 fragment
也没有重新创建,更烦人的是没有恢复(onResume
fragment 的方法永远不会被调用)。
问题:
为什么在通过应用程序使用 intent
时,fragments
不遵循其父 Activity 的生命周期并且至少不恢复?
有办法解决这个问题吗?
还是我完全错了?
最佳答案
使用 Fragment Activity 而不是 Activity。
来自 FragmentActiviy :
Dispatch onResume() to fragments. Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed. This means that in some cases the previous state may still be saved, not allowing fragment transactions that modify the state. To correctly interact with fragments in their proper state, you should instead override {@link #onResumeFragments().
因此,在 FragmentActivity 中操作 fragment 的正确位置是覆盖 onResumeFragments 方法,如下所示:
protected void onResumeFragments(){
super.onResumeFragments();
// YOUR STUFF IS HERE
}
关于android - 使用 Intent 共享后 fragment 未恢复其 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595797/