Android fragment lifecycle when bringing fragment back from background - android

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

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.

how to call OnResume and Onpause methods in fragment?

i have two fragments A and B.here I am Replacing fragment B from A And then fragment A from B in between i am showing toast in onPause method and onResume method but some how its not working could any one explain me why with code ?
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity.
Resumed :->
The fragment is visible in the running activity.
Paused :->
Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
Stopped :->
The fragment is not 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 will be killed if the activity is killed.
for more information read from android docs

Fragment sometimes save variables while on back stack

I have an ArrayList on an fragment that I populate from a server call, if I navigate to other fragment and check the collection size after come back to this fragment on some devices the list is empty and in some not.
What exactly is happens when the fragment go to the back stack?
Acording to documentation
Stopped
The fragment is not 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 will be killed if the activity is killed.
Once stopped, they are eligible to be destroyed.

Fragment and parent activity life cycle together

I was following these four topics Creating a Fragment, Handling the Fragment Lifecycle , Managing the Activity Lifecycle and Pausing and Resuming an Activity. So I am in a little doubt about this.
My question is
If A Activity call B Activity through Intent but A does not call finish() method then A will be in Pause state if B is Transparent or SemiTransparent and in Stop state if B is Opaque. Am I right?
If A Activity contains Fragment F then if A will go to Pause state then F will go to Pause state and if A will be in Stop state then F will be in Stop state too. Am I right?
If A calls B Activity and B is Transparent then A will be in Pause state and F will too. If B call finish() then A will come to Resume state but what will happen to F? will it come to resume from pause? If it is then how and what steps because I have not seen any direct link in Fragment life cycle which indicates onPause() to onResume() directly as Activity can do.
Hope I am able to ask what I want. Sorry for my bad Englsh.
You can't be sure that only onPause will be called on A if B is SemiTransparent or partially visible as I understand it:
Paused
Another activity is in the foreground and has focus, but this
one is still visible. That is, another activity is visible on top of
this one and that activity is partially transparent or doesn't cover
the entire screen. A paused activity is completely alive (the Activity
object is retained in memory, it maintains all state and member
information, and remains attached to the window manager), but can be
killed by the system in extremely low memory situations.
Yes, you are right:
The lifecycle of the activity in which the fragment lives directly
affects the lifecycle of the fragment, such that each lifecycle
callback for the activity results in a similar callback for each
fragment. For example, when the activity receives onPause(), each
fragment in the activity receives onPause().
However, the opposite is not true, meaning that if a fragment receives onStop, that does not guarantee that the Activity's onStop will be called.
I am not quite sure what you mean by your last sentence or how you have tested this. According to the Fragment documentation:
public void onResume ()
Called when the fragment is visible to the user
and actively running. This is generally tied to Activity.onResume of
the containing Activity's lifecycle
It says generally because it depends on how the fragment is handled by the activity.
If A Activity call B Activity through Intent but A does not call finish() method then A will be in Pause state if B is Transparent or SemiTransparent and in Stop state if B is Opaque. Am I right?
Yes true
If A Activity contains Fragment F then if A will go to Pause state then F will go to Pause state and if A will be in Stop state then F will be in Stop state too. Am I right?
Yes correct
If A calls B Activity and B is Transparent then A will be in Pause state and F will too. If B call finish() then A will come to Resume state but what will happen to F? will it come to resume from pause? If it is then how and what steps because I have not seen any direct link in Fragment life cycle which indicates onPause() to onResume() directly as Activity can do.
What you understood is correct, even in this scenario also fragment will be moved from onPause to onResume state just like an activity.
But unfortunately there is not much documentation about this in developer android.
This might be because they wanted to avoid complicated diagrams which can create more confusion.

Task, Back Button, and onSaveInstanceSate method

Hello I am just a little confused after reading these materials on Tasks and Back Stack, Android Developer Guide:
It says:
When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the Back button while in Activity B, Activity A resumes with its state restored.
While in the APIs reference for onSaveInstanceState() method, it says:
One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it.
The above two situations seem identical to me ("press the Back button to Activity A" and "navigate back from Activity B to Activity A"). But I don't understand why while the former says Activity A resumes with its previous state restored while the latter says the particular instance of Activity B will never be restored. Any explanations?
Thanks in advance!
I think the first one is saying that A will be saved so it can be restored, and the second one is saying that B will not be saved because it can't be restored.

Categories

Resources