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.
Related
I have an actionbar on MainActivity level (mainActionBar) and an actionbar on fragment level (fragActionBar). I have 3 fragments (A,B,C). I want mainActionBar to be shown when I'm accessing fragment A and B. On the other hand, I want fragActionBar to be shown when I'm accessing fragment C.
I use replace() when changing between fragments and use addToBackStack to save previous fragment so I can use popBackStack to back to previous fragment. The flow is like this : A<->B <->C. fragActionBar contains up button and if I press the up button it will back to fragment B. mainActionBar doesn't have up button, I use button outside toolbar/actionbar to change fragments between A<->B->C. So only C->B that is using up button.
I have succeeded to show mainActionBar when I'm on fragment A and B. I also have succeeded to show fragActionBar when I'm on fragment C. But when I go back to fragment B, the mainActionBar didn't show up. I try to put supportActionBar?.show() inside onResume(), but it didn't works. I also try to put (activity as AppCompatActivity).supportActionBar?.show() inside setNavigationOnClickListener, still didn't works.
How can I show the mainActionBar when I get back to fragment B after visiting fragment C?
Turns out, I need to use the toolbar I declared in MainActivity by passing the binding from MainActivity to other fragment. It is because I use a different toolbar in other fragment, that's why it won't affect the changes even after I use .show().
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 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.
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 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?