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?
use getActivity() method. Which will return the enclosing activity for the fragment.
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 .
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
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 .
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With