Fragments removed after Activity recreate - android

I am developing an app, where fragments are changing in the container of one Activity. I am using FragmentTransaction.replace(R.id.container, fragment, tag) to switch between fragments.
All fragments has setRetainInstance(true). When I press Home button and go back to app everything is ok, but when I turn on developers option "Don't keep activities" and do the same, I end up with "clean" activity where there is only the first (initial) fragment presents. In other words, after Activity is reinitialised all fragments are lost.
How can I avoid that and save fragments state?

Related

What will be fragment behavior when came from background. Considering fragment is in stack

I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.
Now by Default, activity will display Fragment1.
If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.
Now user goes to background by pressing home button and open the app from tasks.
Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.
Is this expected behavior ?
As you can see on Android Developers, your fragment will be called at onViewCreated().
https://developer.android.com/guide/components/fragments#Creating
The View has to be updated in case you changed the system language or something like that.
Providing a bit more context to what might be happening here.
If both of your fragments are added e.g. via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.
If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.

change activity theme inside the fragment without recreating it

I have an activity which has some fragments and fragments are replaced with each other in activity when some special events happen. when I change the activity's theme, it will be recreated and it will add the first fragment and I will loose the back stack, the problem is that
I want to know that is there any way to start activity and attach the last fragment to it? or can I send back stack Fragments to the recreated activity?
Thanks.

Saving nested fragment sate on back pressed of Android

I'm confused in a fundamental Android fragment lifecycle.
When I press back, none of my nested fragments are saved and the app recreates everything in a parent activity. These nested fragments are inside viewpagers.
Now, if I press the menu button, and come out of the app, it is stored.
Why does it happen? And how do I ensure that even if the user comes out of the app, by pressing back, the nested fragment states are saved, and restored when the user opens the app again.
There is a fragment state pager adapter for viewpager's to save the state of fragments. But you can still not change the fragment life cycle which is dependent on activity life cycle. So if an activity's on destroy is called, all its fragments will be destroyed. So, this might be your case.

Android fragmentTransaction add() and fragment's onPause

I've implemented navigation in my Android app as one Activity and many Fragments.
When I open new child Fragment, I call this:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction() .addToBackStack(null).replace(R.id.fragment_container, f)
.setBreadCrumbTitle(f.getClass().getSimpleName());
tx.commit()
This works quite well, as old fragment isn't destroyed, so when I go back in the back stack, the old fragment retains the scroll position and everything entered on it.
The problem is, in one Fragment I actually need to pause something when I add another fragment above it. I've thought that onPause() will be called, but it doesn't - I suppose that all other fragments live beneath the ones on top of them.
On the side note, I also think that my app gradually slows down as the user navigates deeper. Shouldn't I be replacing the fragment in the transaction instead, so only one is on the top?

Proper way to save and restore fragment states on backstack?

I have looked around and found variations of this question, but all of the answers seem to be ugly hacks. Is there no easy and proper way to achieve this?
Say for instance I have Activity A, which has a FrameLayout that can hold one fragment at a time. Lets say that when the Activity is first started it loads Fragment A into the FrameLayout, which consists of a ListView. When an item is selected in Fragment A it starts up Fragment B. The way I am currently doing this is by simply hiding Fragment A and then adding Fragment B since this preserves Fragment A's state. I am of course also adding this fragment transaction to the backstack.
So now Fragment A exists on the backstack. Say now I go back to my Android home screen and go to another app. While I am doing this, the Android system decides to destroy my application because the system needs more memory. When I navigate back to my application, how am I supposed to properly restore the state of Fragment B, and Fragment A which is currently on the backstack.
I cannot use setRetainInstance() since it does not work for Fragments placed on the backstack.
Essentially what I am trying to do is restore the backstack to exactly how it was before my application was forcefully closed. So Fragment A should be on the backstack (but not showing), and Fragment B should be currently showing. If I hit the back button, it should properly pop Fragment A off the backstack.
Some notes
Since the application was forcefully closed, it's savedInstanceState != null. Same thing holds for the fragments.

Categories

Resources