android - 使用 Intent 共享后 fragment 未恢复其 Activity

标签 android android-intent android-activity android-fragments intentfilter

上下文: 我有一个使用 ActivitiesFragments 的应用。 我暂停它以访问另一个像谷歌翻译。在 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/

相关文章:

c# - 在 Xamarin Forms 中应该使用哪个包而不是 Android.Content 来获取类 Intent?

android - 如何将值从一个 Activity 传递到上一个 Activity

安卓。像 Telegram 一样滑动返回 Activity

java - 如何在自定义ListView中使用Intent

java - 让家庭 Intent 转向特定的应用程序 Activity ?

android - 启动系统上的任何 Activity

android - 如何将 Intent 从库模块中的 Activity 发送到主应用程序的 Activity

android - Android-Activity和Android-Fragment的关系是什么; UML 类图中的 Activity/Fragment 和 ViewModel?

c# - Retrofit + Restful c# 上传文件

java - 如何将 textview 重置为其默认值?