Fragment sometimes save variables while on back stack - android

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.

Related

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

Force onDestroyView on a fragment's view

I'm writing an app that loads user profiles and user's ratings for different places. The app uses fragments pervasively, and it's relatively easy to jump from a profile to a rated place.
As a user clicks a profile and gets to a rated place, they can click another profile on a rated place and go on and on.
The problem I'm having is memory related, when I'm looking at a ranked place and I click a profile, I switch from one Activity to the next. In both of these activities, after setContentView I load a fragment dynamically into layout space.
Now, as I shuttle between these activities, onSaveInstance state is almost always called, however since the Fragment displaying whatever was in the foreground before the activity switch, onDestroyView is not called.
What I would like is when onSaveInstanceState is called in these dynamic fragments, it to force onDestroyView to be called as well.
The only time onDestroyView seems to be called is when I add a Fragment to the back stack. When another activity comes to the foreground and this fragment is stopped, I'd like on DestroyView to be called as well.
The current workaround I want to implement is have an empty fragment with no view, and every time I call startActivity(Intent i), load this dummy fragment to destroy views and start the next activity. Then, when I come back, pop it off the back stack and restore the actual last fragment.
This however seems excessive. So, for a stopped fragment in a stopped activity with a new activity in front of it, how do I force it to destroy it's View?
First, you should not force or satisfy onDestroyView to fix your code, that's the job of the FragmentManager and the Android lifecyle # Pausing and Resuming an Activity. If you want to work with your existing code, use the other override methods onPause() or onStop().
Without posted code, I assume you're using the replace() method to display one fragment over another. This more or less forces you to manage the fragments yourself, some developers actually succeed in doing so with some struggle (look at other SO questions).
So my suggestion for you is either:
Maintain your own states, and show the proper fragments based on the state.
Use the BackStack and let the Fragment management handle the stack/states.

How to remove fragment from fragment manager when activity is restored after being destroyed

I have an activity A which contains fragment F. A shows F by pushing it onto fragment manager's back stack. This fragment may show a dialog(more specifically, a DialogFragment) D, also by pushing it to the same fragment manager's back stack.
I need to be able to dismiss dialog D under certain circumstances that are determined by fragment F. Normally I would check if D is on the fragment manager's back stack and use getFragmentManager().popBackStack() to remove it. But this doesn't work if the activity gets destroyed and then recreated:
Say I set "Don't keep activities" flag in Android Settings. Now I background the app. Activity gets destroyed, and the fragments are too. Now I foreground the app again. At what point do the fragments F and D get added to fragment manager's back stack? This is a screenshot I took after I put a breakpoint on A's onPostResume() method, which I assume is the very last one to run in the activity lifecycle, before the user can use the app:
You may notice that mAdded field contains 2 elements - those are the restored fragments F and D. But they are not on the back stack yet, as mBackStack is null!
I would like to be able to remove D, but Android won't let me do it, since it's waiting to restore pre-existing state of fragments and it won't add them to the back stack until some time after onPostResume.
So in essence, I can't remove the fragment from the stack, since it's not on the stack yet. And I also can't prevent it from being added to the stack at some point, since, as you can see from the screenshot above, fragment manager stores it in a separate list mAdded and there's no methods that I can use to remove it from mAdded.
How can I prevent a saved fragment from being restored?
Maybe Im wrong but AFAIK AOS doesn't store the fragment backstack at all if activity stops. It could only restore the last shown fragment without all the previous fragments on a stack.
However you could store the stack and fragments state yourself. Just remove your dialog in onPause storing some flag via onSaveInstanceState and then in onResume restore it or don't.

Is it possible to still interact with Fragments on the back-stack?

Referring to the Fragment documentation, specifically Performing Fragment Transactions it states:
Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and will be resumed if the user navigates back.
I want to understand the implications of a Frgament being stopped because I am creating several Fragments which I would like to place on the BackStack yet must still interact with them from an external (i.e. non-GUI) thread, to set a flag within the Fragment for example. Also if the Fragment starts threads within it, are they too stopped or 'suspended' once the Fragment is placed onto the backstack?

Is the Fragments backstack also restored when the activity is recreated after its process being killed?

Since the fragments back stack is managed by the host activity, if this activity is destroyed by the system (because the system needs more memory somewhere else) and recreated after because the user came back, will the fragments back stack state be recreated also by the system?
I dont believe the fragment backstack is saved by default. However you can request it be added with this:
FragmentTransaction.addToBackStack()

Categories

Resources