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?
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 :
Related
According to Android Developer site, the correct way of communicating an activity with their fragment is through listeners.
https://developer.android.com/training/basics/fragments/communicating
My question is, this fragment is holding a reference to the activity... when the activity is destroyed, will the fragment manager release the fragment and thus the fragment will be collected and so the activity? or do they hold a strong reference that needs to be nullified too in the Fragment's onDestroy?
The Fragments Lifecycle is bound to the one of the Activity. Imagine an Activity as the Universe and Fragments as Planets / Stars. If the Universe dies, so do the Stars / Planets inside of it. Similarly, if an Activity gets destroyed so do all of it's Fragments.
The official documentation (which you should definitely check out) explains it very well:
A fragment must always be hosted in an activity and the fragment's
lifecycle is directly affected by the host activity's lifecycle. For
example, when the activity is paused, so are all fragments in it, and
when the activity is destroyed, so are all fragments. However, while
an activity is running (it is in the resumed lifecycle state), you can
manipulate each fragment independently, such as add or remove them.
When the activity containing the fragment is destroyed, so is the fragment automatically. Check this out
Everywhere it is written that fragments have their own lifecycle . Also fragment life cycle depend on activity's lifecycle.What is the meaning of fragment's own lifecycle if it is dependent on activity's lifecycle?
First of all you need to understand what are lifecycle methods are and when are they called/invoked. Lifecycle methods are basically invoked at the different state of your Activty/Fragment. For example when you first launch your activity the following flow of events/methods are called depending upon the state of your activity. For example : When your activity is first launched OnCreate is called, when your activity is no longer visible then onStop is called. So basically you first need to learn at which state are these different activities called.Below is a great referential flowchart for the same.
Activity lifecycle methods :
Fragment lifecycle methods :
Now, when you create a fragment it is inflated into the activty. And it has its own set of lifecycle events/methods which are called and since the fragment is inflated into the activty when the state of your activity changes it effects the fragment and correspondingly different lifecycle methods of the fragments are called. Below is another pictorial representation of the relation between the lifecycle methods of the activity and the fragment.
Image source : Google Images
The activity lifecycle is fairly simple in comparison to the fragment lolcycle (image from Square's Advocating against Android Fragments)
What fragment lifecycle methods (onCreate, onViewCreate, onStart) are called after you setup ViewPager with, say, 10 fragments and call setCurrentItem(9)?
Will there be any difference if you call setCurrentItem(i, false)?
What fragment lifecycle methods (onCreate, onViewCreate, onStart) are called after you setup ViewPager
Every lifecycle method you mentioned plus onAttach, onResume, and onActivityStarted are called. There is nothing about a ViewPager that affects that. Refer to this picture to see the Fragment lifecycle.
Will there be any difference if you call setCurrentItem(i, false)?
No, false does not affect the lifecycle. the only thing that false will do is transition immediately to the page instead of smoothly across the ViewPager.
The onPause, onStop, etc. methods may be called after you scroll out of view of a Fragment, but that was not part of your question. ..
I would like to understand the following:
If we do a design of an activity that the UI is composed by multiple "child" fragments of that activity, if 1 of those fragments does something "costly" in the UI thread in the onCreateView does this affect all of the fragments?
Because if I understand correctly onCreateView is called only once
Yes it will affect all the Fragments if the Actvity they are attached to is affected.
According to Android Developer Guide on Fragment, once the Fragment is attached to the Activity, it's Life Cycle is controlled by the Activity e.g. if Activity is Destroyed the Fragment is too.
Fragment is just a like a little Activity so if there is possibility that fragment operation is likely to be long then AsyncTask or any other suitable service should be used. See below link for more details on Background Services;
https://developer.android.com/training/run-background-service/create-service.html
yes
it is effect all of the app
I have an activity which contains 2 fragments. I want to save certain state of the activity and also of the fragments, in order to restore if the activity, or the fragments, are destroyed.
So I'm using onSaveInstanceState both in the fragments and in the activity, and take the data of the bundle passed to onCreate or onCreateView.
This works well besides, when the activity is destroyed. Then in restores it's own data, but, since in onCreate() I instantiate the adapter and the fragments again, they get no state.
How do I solve this?
Thanks in advance.
Most likely the cause of this, is that the Fragment's onCreateView() runs before the Activity's onResume() according to the Fragment lifecycle documentation:
http://developer.android.com/guide/components/fragments.html#Creating