我怎么可以打破东西的碎片与setRetainInstance(真),并把它们添加到backstack? [英] how i can break things with Fragments with setRetainInstance(true) and adding them to backstack?

查看:112
本文介绍了我怎么可以打破东西的碎片与setRetainInstance(真),并把它们添加到backstack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在setRetainInstance文档说:

the docs on setRetainInstance say :

这只能用于与碎片不会在背面叠层

This can only be used with fragments not in the back stack.

所以我就开始摆弄它。

我有一个活性的增加先断枝A

I have one Activity with adds first frag A

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, new PackageFragment());
ft.commit

然后再从这个断枝我运行父活动的方法,其增加的frag B至backstack

then from this frag I run a method from parent Activity which adds frag B to backstack

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, new OrderFragment());
ft.addToBackStack(null);
ft.commit();

然后我从创建日志味精的onCreate,的onDestroy,的onSaveInstanceState,onActivityCreated ...等

then I create log msg from onCreate,onDestroy,onSaveInstanceState,onActivityCreated...etc

我尝试两个版本的过程。旋转设备的每个片段上。

I try two versions of this process. Rotating the device on each fragment.

  1. 默认

一切都如预期。的onCreate,对的onDestroy火片段

Everything is as expected. onCreate, onDestroy on fragments fire

  1. setRetainInstance(真)

一切都如预期?的onCreate,对的onDestroy碎片不火

Everything is as expected?. onCreate, onDestroy on fragments dont fire

和一切似乎工作,而片段在backstack ..所以,为什么文档说我不应该使用它吗? 是什么,我可能会遇到麻烦的情况下?

and all seems to work while fragments are in the backstack.. so why the docs say I shouldnt use it? What are the scenarios where I might get in trouble?

感谢

推荐答案

更​​新答案:

什么是在那里我会遇到麻烦的情况下?

What are the scenarios where I might get in trouble?

在添加片段到后面堆和片段传递捆绑 的onSaveInstanceState() onCreateView()的配置变化。呼叫 setRetainInstance(真)将设置捆绑来对配置更改为空。

When adding a Fragment to the back stack and passing a Bundle in the Fragment from onSaveInstanceState() to onCreateView() on configuration change. Calling setRetainInstance(true) will set the Bundle to null on configuration change.

(我不知道,因为使用 setRetainInstance(真)的onSaveInstanceState()那种多余的,但我没有看到记录在API文档,所以我写了这个答案的行为)。

(I'm not sure a developer would actually attempt this since using setRetainInstance(true) makes onSaveInstanceState() kind of redundant, but I didn't see the behaviour documented in the API docs so I wrote up this answer).

如果两个 addToBackStack() setRetainInstance(真)被称为, setRetainInstance( )部分改变了片段的配置更改,相比之下,只有调用 addToBackStack()<生命周期方法调用和参数值/ code>。

If both addToBackStack() and setRetainInstance(true) are called, setRetainInstance() partly alters the Fragment lifecycle method calls and parameter values on configuration changes, compared to calling only addToBackStack().

具体而言,在下面的测试中,只查看调用之间的差异 addToBackStack()并调用 setRetainInstance(真)为好,看到的配置变化会发生什么:

Specifically, in the test below, looking a differences between calling only addToBackStack() and calling setRetainInstance(true) as well, and seeing what happens on configuration change:

调用 addToBackStack()而不是 setRetainInstance(真);

  • 的onCreate()的onDestroy()被调用。
  • 的onSaveInstanceState()被接收为一个参数 onCreateView()
  • 通过捆绑
  • onCreate() and onDestroy() are called.
  • a bundle passed from onSaveInstanceState() is received as a parameter in onCreateView().

调用这两个 addToBackStack() setRetainInstance(真)

  • 的onCreate <$ C C $>()的onDestroy()不叫。这是在文件档案的API文档。
  • 的onSaveInstanceState通过捆绑()未在 onCreateView接受()。传入的​​捆绑为空。
  • onCreate() and onDestroy() are not called. This is metioned in the API docs.
  • a bundle passed from onSaveInstanceState() is not received in onCreateView(). The passed-in Bundle is null.

与测试空记录的方法调用和参数测试:

A test with logged method calls and parameters tested for null:

活动

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyFragment fragment;
    if (savedInstanceState != null) {
        fragment = (MyFragment) getFragmentManager().findFragmentByTag("my_fragment_tag");
    } else {
        fragment = new MyFragment();
        FragmentTransaction t = getFragmentManager().beginTransaction();
        t.addToBackStack(null);//toggle this
        t.add(android.R.id.content, fragment, "my_fragment_tag").commit(); 
    }
}

片段

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);//toggle this
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("test", "value");
    super.onSaveInstanceState(outState);
}

测试1:片段生命周期,当 addToBackStack()被调用,而 setRetainInstance(真)没有名为

  • onAttach()
  • 的onCreate()
  • onCreateView()
  • onActivityCreated()
  • ONSTART()
  • 在onResume()

[从纵向到横向旋转设备]

[Device rotated from portrait to landscape]

  • 的onPause()
  • 的onSaveInstanceState()
  • 的onStop()
  • onDestroyView()
  • 的onDestroy()
  • onDetach()
  • onAttach()
  • 的onCreate()
  • onCreateView()与束参数!= NULL
  • ONSTART()
  • 在onResume()
  • onPause()
  • onSaveInstanceState()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()
  • onAttach()
  • onCreate()
  • onCreateView() with bundle param != null
  • onStart()
  • onResume()

试验2及3: setRetainInstance片段生命周期调用(真) addToBackStack()叫/不叫(相同的结果):

Test 2 & 3: Fragment lifecycle calls with setRetainInstance(true) called, addToBackStack() called / not called (same result):

  • onAttach()
  • onCreateView()
  • onActivityCreated()
  • ONSTART()
  • 在onResume()

[从纵向到横向旋转设备]

[Device rotated from portrait to landscape]

  • 的onPause()
  • 的onSaveInstanceState()
  • 的onStop()
  • onDestroyView()
  • onDetach()
  • onAttach()
  • onCreateView()与束参数== NULL
  • ONSTART()
  • 在onResume()
  • onPause()
  • onSaveInstanceState()
  • onStop()
  • onDestroyView()
  • onDetach()
  • onAttach()
  • onCreateView() with bundle param == null
  • onStart()
  • onResume()

这篇关于我怎么可以打破东西的碎片与setRetainInstance(真),并把它们添加到backstack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆