Questions about Activity and Fragment lifecycle - android

I trace lifecycle method's callback from Activity and Fragment components and i have a lot of questions. I need you help to understand some points.
Situation 1:
One launcher Activity with one Fragment, which filled it. When app started, Fragment become active and user see only Fragment.
Lyfecycle:
Activity: onCreate
Activity: onStart
Fragment: onAttach
Fragment: onCreate
Fragment: onCreateView
Fragment: onViewCreated
Fragment: onActivityCreated
Activity: onResume
Fragment: onResume.
Questions:
1) There is no onResume between 2 - 3, because of Activity lose focus?
2) I don't think i understand what happen on 8 - 9. It seems like some kind of "switch focus" between Fragment and Activity. Ok, i can understand: 9 is about Fragment came to the foreground, take focus and onResume was called. But what is 8? When i load Fragment(since 3), Activity is never on foreground.
Situation 2:
Like situation 1, but now Fragment and Activity loaded and i pressed Home:
Activity: onPause
Fragment: onPause
Activity: onStop
Fragment: onStop
Questions:
When i run it, i expect another behavior. I thought Fragment should be the first who called stop methods, like (Fragment onPause -> Fragment onStop - > Activity methods). Why it worked this way?
Situation 3:
Like situation 2, Activity with Fragment are back to the foreground. I remember, they both have Stopped status (because of onStop was called).
Activity: onStart
Activity: onResume
Fragment: onResume
Questions:
I expect 1-2, but i expect that Fragment call onStart, because, if i understand it correct, onStart called when fragment become visible. Why onStart was't called?

Fragment and Activity lifecycles work in parallel. The linear dependence between their lifecycle ends as soon as both activity and fragment are created.
The following figure explains how the two lifecycles are interconnected.
Note that onStart and onResume (and similarly, onStop and onPause) for both fragment and activity execute in parallel and there is no guarantee of order. Sometimes fragment will take precedence over activity, and vice versa.
The only guarantee is that activity's onCreate will always be called first. After that, fragment acts on its own.

Related

why onCleared in my viewModel is called when I close the fragment using navigation component?

I have tried to read this, but I think I cant find the solution.
say I have FragmentA and Fragment B. I move to fragment B using this code
val nextDestination = AFragmentDirections.actionToB()
findNavController().navigate(nextDestination)
when I back from fragment B to fragment A, onDestroy in my fragment is called and then onCleared on my ViewModel is also get called.
but when I rotate the phone (configuration changes), when onDestroy is called, then onCleared is NOT called.
so I previously assume, when onDestroy called, then onCleared is also called. but it is not. why there is different behaviour like this ? when configuration changes is different from fragment navigation ?
I am confused when exactly the onCleared is called
The whole point of a ViewModel is to survive configuration changes as per the ViewModel documentation, so it is expected that ViewModels are not cleared over a configuration change.
However, each ViewModel is associated with a ViewModelStoreOwner. This could be your activity or it could be a Fragment. It is up to that ViewModelStoreOwner to understand when it is being destroyed due to a temporary change (such as a configuration change) or a permanent destruction. It is only in that permanent destruction that onCleared() for each ViewModel is called.
For an activity, that permanent destruction happens when the activity is finished (i.e., finish() is called or the behavior of onBackPressed() leads to finish() being called).
For a Fragment, that permanent destruction happens when the Fragment is popped off the back stack. This is what happens when you pop fragment B off the back stack and return to Fragment A - Fragment B is removed from the FragmentManager, it goes through onDestroy(), and its ViewModels are cleared.
Fragment A will have the same thing happen to it once it is popped off the back stack or, if it is the only thing on the back stack, when the activity is finished when you hit the system back button.

Android fragment lifecycle when bringing fragment back from background

Scenario: fragment is visible, user minimises the app and brings it back from background.
Why onCreateView onCreate is not being called and it jumps straight to calling onStart?
When is onCreate onCreateView called? How to restore fragment data if it jumps straight to onStart?
as stated here:
https://developer.android.com/guide/components/fragments#Lifecycle
"The fragment isn't visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and is killed if the activity is killed."
And you can save the data using the OnSaveInstanceState(Bundle) on the onStart() method

Will Activity onpause be called when switching between fragments

Right now I have 2 activities. When the second activity is running, the first activity onpause is clicked which means I have to unregister all the listeners (which is not what I want when the app is running, I just wanted them unregistered when the app is in the background).
So would it make sense for me to create 2 fragments with one activity. Then I can unregister the listener when the activity onpause is called (only when app goes to background) but it won't be called when fragments are switched.
Is my understanding correct?
Thank you
This totally depends on the activity that you have used to launch your fragment . If two fragments A and B belong to the same activity, then the moment you switch view from Fragment A to Fragment B, your activity does not go to a paused state, means onPause() won't be called, but is still running which is ideally providing the view for Fragment B.
So, you do not need to unregister the listener on change of Fragment in the same activity.
Please correct me if I am wrong.
Yups Activity onPause() would not be called when switching fragments.

onDetach not called for fragment?

I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation.
Scenario: If Activity A frequently launch Activity B which contain Fragment, then most of times it missed Fragment.onDetach..I checked with log, normally it give me following override method log:
onAttach
List item
OnCreatView
onViewCreate then press device Back Button
onPause
onStop
onDestroyView
onDetach
now I press device Back button from Activity B which again launch Activity A then it launch Activity B and repeat same sequence frequently, then log sequence get change in following order:
onAttach
List item
OnCreatView
onViewCreate then press device Back Button
onPause
onStop
onDestroyView and repeat with
onAttach without onDetach
some times it repeat same behaviour after onPause also.
I am using
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss(); to add fragment in Activity.
Is there anything I am missing..any suggestion ?
A fragment is detached after it is destroyed. what u have done is dettached directly after destroying view.
Remember destroying and destroying view are two different things in fragment.
So try onDestroyView, then onDestroy and then onDetach.

android -does calling finish from activity destroy hosted fragment?

I have an 'Activity A' which host a 'Fragment B'. Please confirm that if I call A.finish() then 'fragment B 'gets destroyed. The fragment B would already be added to the fragment manager. I realize that a fragment is a sub-activity but i want to know do i have to tell the fragment manager to release the fragment in Ondestroy or is it all taken care of ?
but i want to know do i have to tell the fragment manager to
release the fragment in Ondestroy or is it all taken care of ?
By default when you call finish in your activity it will automatically call onDestroy method of all the fragments that is attach to the activity thus destroying it, so no need to worry about destroying the fragment in onDestroy method of your activity.
From the documentation:
For example, when the activity is paused, so are all fragments in it,
and when the activity is destroyed, so are all fragments.
Fragments are the Subactivities of an activity. So whenever we will call the finish the fragment associated with it will also get destroyed without notifying FragmentManager.
Yes the fragment is destroyed. From the Android Fragment guide:
For example, when the activity is paused, so are all fragments in it,
and when the activity is destroyed, so are all fragments.

Categories

Resources