change activity theme inside the fragment without recreating it - android

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.

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.

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.

Up navigation with fragments and activities

I'm trying to use the up navigation pattern and I'm having issues while using both fragments and activities. Let's say that I have activity A and that activity contains fragment a. Now I click something in fragment a and a fragmentTransaction happens with fragment b replacing a and a is put in the back stack. The "up" arrow now appears in my toolbar. This is all fine. Now I click something in fragment b and fire up activity B. A is B's parent. Now, B has an up arrow and I expect that when I touch it I would go to the top, i.e. finish activity B and the back stack of A would be popped so we end up with activity A containing fragment a and empty back stack.
My issue here is that when up is pressed in B, B is finished but A is still showing fragment b. Is there any way that I can make A not restore its' fragment back state onActivityResult or something like that so fragment b is never shown on up action from B?
What I have tried is using startActivityForResult when firing up B and popping A's fragment backstack onActivityResult but then b is briefly shown before the stack is popped. I just want A not to restore it's fragment state if up was pressed in B.
Edit:
The pattern I'm using right now for the up navigation is that in my manifest I define A as B's parent and in activity B I have a toolbar which I set as a supportActionBar with setDisplayHomeAsUpEnabled.
I had my activity defined as "singleTop" in my manifest. If you remove that the up button causes the task to be recreated which is exactly what I wanted.
Edit: The pattern I'm using right now for the up navigation is that in
my manifest I define A as B's parent and in activity B I have a
toolbar which I set as a supportActionBar with
setDisplayHomeAsUpEnabled.
said that my suggestion is popping A(b) when going to B.
When you call navigateUpFromSameTask it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. That's why you are experiencing that behaviour.

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.

Android Layout is overlapping itself

I am using fragments in my app. Below is the screenshot of my app. Everything looks fine on activity launch. But, when I change the screen orientation, the layout overlaps itself while scrolling. Any ideas what is wrong?
EDIT (Solution):
I found out what was wrong. A new fragment was added on orientation change. I was previously using fragmentTransaction.add(...), I replaced it with replace method.
For future vistor's understanding...
You are most likely adding your fragment in your activity's onCreate().
When your device is rotated, your activity is destroyed and re-created, but it also recreates any added fragments.
With these two things in mind...
onCreate is called and you add a fragment to your activity. You have one fragment.
You rotate the screen.
The activity is destroyed and re-created. Your fragment is destroyed and recreated. You still have one fragment.
Your activity calls onCreate() as a part of its recreation, which adds a fragment to your activity. You now have two fragments.
If one or both of your fragments have transparent backgrounds, both may display at once, as is likely the case here.
Either use replace instead of add (which you have done), or better, only add the fragment in onCreate() if savedInstanceState is null. If it is null it means it's the first onCreate() call.

Categories

Resources