【问题标题】:How to prevent a child fragment to be automatically created after app restart?如何防止应用重启后自动创建子片段?
【发布时间】:2016-08-30 13:04:38
【问题描述】:

我的应用有一个 Activity A,其中包含一个 Fragment B。在 B 中,用户单击一个按钮,然后在 B 中创建一个子 Fragment C。现在用户通过按主页按钮使应用后台运行。一段时间后,我的应用程序被 Android OS 杀死并重新启动。

然后用户再次启动应用程序。现在 Activity A 已启动,并且 Fragment B 及其子 Fragment C 都将自动重新创建。我的问题,如何防止子片段 C 自动重新创建?重启时我尝试在B的onAttachFragment方法中从B中删除片段C,但是没有成功。

@Override
public void onAttachFragment(Fragment f) {
  super.onAttachFragment(f);
  if (isRestart) {
    getChildFragmentManager().beginTransaction()
      .remove(f)
      .commit();
    isRestart = false;
}

}

【问题讨论】:

标签: android android-fragments


【解决方案1】:

好的。我最终在 B 的 onActivityCreated() 中从 B 中删除了子片段 C。之后只调用了C的onCreate(),没有调用onCreateView、onStart、onResume。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多