Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can some draw lifecycle of Fragment and its parent FragmentActivity?

In Android docs, I found a specs on Activity lifecycle and on Fragment lifecycle individually, but never together. It does not seem obvious as I attached a debugger to FragmentActivity which hosts my fragment, and the life cycle is more than crazy. It looks like activity finishes first and then fragments starts, which is impossible.

Fragment's lifecycle


Activity's lifecycle


Logically, fragment should "jump into" activity's lifecycle after its onResume and it would end before activity's onPause, but it seems it is not happening.

Can someone either show me the lifecycle of the fragment in connection to its parent's activity or direct me to some good tutorial on this?

like image 465
sandalone Avatar asked Dec 18 '12 21:12

sandalone


People also ask

How do you get FragmentActivity in a fragment?

use getActivity() method. Which will return the enclosing activity for the fragment.

Does a fragment have its own life cycle?

These include onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . A fragment's view has a separate Lifecycle that is managed independently from that of the fragment's Lifecycle .

What is the difference between fragment and FragmentActivity?

FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.

Is Fragment life cycle dependent on activity life cycle?

Because a Fragment is always hosted by an Activity , the Fragment lifecycle is directly affected by the host Activity lifecycle. For example, when the Activity is paused, so are all Fragments in it, and when the Activity is destroyed, so are all Fragments .


1 Answers

Have you seen this? Fragment is created after Activity is created. It doesn't seem possible for "activity finishes first and then fragments starts" Can you post the code for that?
This is what I tested, 1 FragmentActivity , two Fragments :enter image description here

like image 197
wtsang02 Avatar answered Oct 13 '22 12:10

wtsang02