I have an activity. When it starts it loads Fragment A in onCreate using replace. This is not added to the back stack.
I then want Fragment B, an overlay, to be added on top of the previous fragment. This is because i still want you to still be able to see Fragment A behind it as Fragment B has a translucent background. This is loaded using add and is added to the back stack.
From Fragment B i load Fragment C using replace and add to the back stack. This is because Fragment C is not an overlay like Fragment B. When i'm in Fragment C i want to go back to Fragment B on back press. I override on back press and pop the back stack.
The issue is when i press the back button i see Fragment A and not Fragment B. Even though i have debugged and can see Fragment B in the back stack. Why is this?
Related
I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!
Imagine i have a frameLayout in my main activity, and two fragments,
so now first fragment is being displayed, i replaced it with secondFragment and added first one to back stack, now some condition is met in the secondfragment so i want to close the second fragment and open the first one where we left that (basically back from the back stack) without pressing the back button. how do i achieve this ?
you can write this code for removing current fragment from backstack:
getActivity().getSupportFragmentManager().popBackStack();
I am having a situation Fragment A is used Add but in FragmentB I used replace for FragmentC.
FragmentA--Add---FragmentB---Repace --FragmentC
When i clicked back from fragment C it brings me back to Fragment A.
Note : all fragments added in backstack
Required behavior : When pressed back from fragmentC it must come back to FragmentB. Instead it brings back to fragmentA
Plz help.
Regards
This was asked in one of the interviews. The scenario is described as follows :
MainActivity (initially has Fragment A added to it.). On pressing a button, it goes to Fragment B, without adding A to backstack. On pressing a button in Fragment B, it adds 'B' to back stack, and goes to Fragment C. Now, in fragment C, I have a button through which I directly want to navigate to fragment A (which wasn't added to back stack). Is there any way possible to do this?
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.