Дальнейшее понимание setRetainInstance (true)
Какиеexactly происходит, когда вы звонитеsetRetainInstance(true)
наFragment
? Документация практически не существует, и это кажется очень важной функцией. В частности, я хочу знать, какая часть этой последовательности (которую я составил) верна:
Activity
and Fragment.onDetach()
is called.
The activity is destroyed; Activity.onDestroy()
is called.
The Activity
java object is deleted (when possible, by the GC).
A new Activity
java object is created; its constructor, and onCreate()
are called.
In Activity.onCreate()
we either have setContentView(...)
which sets a layout containing a fragment, or we use FragmentTransaction
to add a fragment.
I'm really not sure about this, but I assume that android is smart enough to find the old fragment, and call Fragment.onAttach()
to
reattach it to the new Activity
Next (or before? who knows?) Activity.onResume()
is called.
Так это правильно? Является ли Android достаточно умным, чтобы найти старый фрагмент, даже если я явно используюFragmentTransaction.add(new MyFragment(), ...)
первый раз? И если да, то как мне избежать добавленияanother фрагмент вonCreate()
? Нужно ли делать что-то подобное?
if (getSupportFragmentManager().findFragmentByTag("foo") == null)
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(new FooFragment(), "foo").commit();
}