Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityCreated is always called?

Looking at the excellent diagram at https://github.com/xxv/android-lifecycle it says that onActivityCreated() is not called on fragment restart.

I've doubts about that:

  • Is that true? Someone can provide some link to do documentation where explains that lifecycle behaviour?
  • Exactly what is Fragment restart()?
  • Can Android decide to delete not visible fragments but mantain the activity that holds them?

Note 1: I've tested that onActivityCreated is called when Fragment is added due to activity recreation and also when the fragment is added manually after the activity is completely started and active.

Note 2: I'm testing with 23.3.0 support version. Is it possible that some behaviour has changed from previous versions?

like image 538
lujop Avatar asked Apr 26 '16 08:04

lujop


1 Answers

Most methods of the host Activity are mirrored by Fragments.

onRestart() called when your Activity in a back stack and you hit back button, so there is no need for re-creation (it just re-starts).

Some methods doesn't exist in Fragment (like onRestart(), onRestoreInstanceState(), don't know why). So restart happens in the host and it triggers Fragment's onStart(), onResume(), just think it restarts with Activity.

Another story with attachment and detachment. In some callback you want safely work with view tree and onActivityCreated() is a good candidate, so it's not restart.

I guess Fragment doesn't have onRestart() because it's up to developer to manage the back stack and you can have retained Fragments (which you shouldn't put in the back stack).

The best prove is the code. Try to play with logging. Some time ago I did it myself, because Steve's project tests only one Fragment.

Pushed a little mode (start ActivityWithDynamicFragments, then push back button) and here is a log:

 176448881: DynamicFragment.<init> / ctor
 176448881: DynamicFragment.onAttach / in base with context
 176448881: DynamicFragment.onAttach / in base with activity
 176448881: DynamicFragment.onAttach / out base with activity
 176448881: DynamicFragment.onAttach / out base with context
 176448881: DynamicFragment.onCreate / in base
 176448881: DynamicFragment.onCreate / out base
 176448881: DynamicFragment.onCreateView / in brand new
 176448881: DynamicFragment.onCreateView / out
 176448881: DynamicFragment.onViewCreated / in base
 176448881: DynamicFragment.onViewCreated / out base
 176448881: DynamicFragment.onActivityCreated / in base
 176448881: DynamicFragment.onActivityCreated / out base
 176448881: DynamicFragment.onViewStateRestored / in base
 176448881: DynamicFragment.onViewStateRestored / out base
 176448881: DynamicFragment.onStart / in base
 176448881: DynamicFragment.onStart / out base
 176448881: DynamicFragment.onResume / in base
 176448881: DynamicFragment.onResume / out base
 176448881: DynamicFragment.onPause / in base
 176448881: DynamicFragment.onPause / out base

 227303269: ActivityWithDynamicFragments.onCreate / in brand new
 227303269: ActivityWithDynamicFragments.onCreate / in  base
 227303269: ActivityWithDynamicFragments.onCreate / out  base
 227303269: ActivityWithDynamicFragments.onCreate / after super
 227303269: ActivityWithDynamicFragments.onContentChanged / in base
 227303269: ActivityWithDynamicFragments.onContentChanged / out base
 227303269: ActivityWithDynamicFragments.onStart / in base
 227303269: ActivityWithDynamicFragments.onStart / out base
 227303269: ActivityWithDynamicFragments.onResume / in base
 227303269: ActivityWithDynamicFragments.onResume / out base
 227303269: ActivityWithDynamicFragments.onPostResume / in base
 227303269: ActivityWithDynamicFragments.onResumeFragments / in base
 227303269: ActivityWithDynamicFragments.onResumeFragments / out base
 227303269: ActivityWithDynamicFragments.onPostResume / out base
 227303269: ActivityWithDynamicFragments.onAttachedToWindow / in base
 227303269: ActivityWithDynamicFragments.onAttachedToWindow / out base

 176448881: DynamicFragment.onSaveInstanceState / in base
 176448881: DynamicFragment.onSaveInstanceState / out base
 176448881: DynamicFragment.onStop / in base
 176448881: DynamicFragment.onStop / out base
 227303269: ActivityWithDynamicFragments.onPause / in base
 227303269: ActivityWithDynamicFragments.onPause / out base

D/NSA: onRestart
 176448881: DynamicFragment.onStart / in base
 176448881: DynamicFragment.onStart / out base
 176448881: DynamicFragment.onResume / in base
 176448881: DynamicFragment.onResume / out base
 227303269: ActivityWithDynamicFragments.onStop / in base
 227303269: ActivityWithDynamicFragments.onStop / out base
 227303269: ActivityWithDynamicFragments.onDestroy / in base
 227303269: ActivityWithDynamicFragments.onDestroy / out base

We have two terms recreation and restart.

Recreation happens only with modification (even potential, when Fragment without UI) of the view tree.

Restart happens when your view tree is stable (ex. your activity with fragment in the back stack and you want to fetch it back).

Can Android decide to delete not visible fragments but mantain the activity that holds them?

I believe that the only manageable quanta for killing by Android OS is a process (but it's debatable).

As for Fragment you can check source code of FragmentActivity and FragmentManager (I don't see such flow).

like image 127
Maxim G Avatar answered Sep 23 '22 01:09

Maxim G