How to remove previous fragment from back stack - android

I'm working with the fragments navigation and have a problem removing one from the back stack. Here're specifics:
I have 3 independent fragments (let's name them A1, A2, and A3) and each of them can navigate to fragment B using findNavController().navigate(), which after some actions navigates to fragment C using the same method.
What I need is when the back arrow is pressed I return to the A-fragment(which I started from) avoiding fragment B.
I've tried using app:popUpTo, and popBackStack but it hadn't worked. Also, I'm highly not recommended to use FragmentManager

You can use
findNavController().popUp(2)

Related

Android. The fragment is shown for a few seconds when it receives a result in order to navigate to a new screen

Fox example, I have activities A, B, C. Each of these activities is a container for fragments F1, F2, F3. Also, I have another fragment - F4. This fragment which can be opened from fragments F1, F2, F3. In addition, from fragment F4 I need to perform navigation to activities A B and C and also remove from back stack fragment F4. Now I close the F4 fragment for this and using the fragment result API I pass the result to the previous fragment, and from the previous fragment I perform the navigation to the activity I need.
But in this case, the previous fragment appears on the screen for a few seconds, but I would like it to look like I navigated from fragment F4.
Is it possible to avoid this behavior ?
Please, help me ?

Fragment Stack Management

I need some help in Fragment stack. I have a scenarios. In that scenarios, We have four fragments A , B , C and D. I have added all the fragments in back stack using add method of FragmentTransaction. I want to swipe D to B without removing D in stack. As I have used below method for that. But it is removing D in stack.
appCompatActivity.supportFragmentManager.popBackStack(fragmentName, FragmentManager.POP_BACK_STACK_INCLUSIVE)
why don't you use viewpager?
The easiest way to work with ViewPager

Cannot update fragment's data (fragment in viewpager)

I have 3 activites- A,B, and C. All these 3 activities have a viewpager. And these viewpagers have 1 fragment(this fragment is common to all viewpagers). The launcher activity is A, and pressing a button in A takes me to B, and pressing a button in B takes me to C.
A-> B -> C
Now the problem is when I update any data in C's fragment, it is not updated in A's and B's fragment. But if I update any data in A's fragment and then go to B, B's fragment will have updated data but vice versa is not happening.
Note: I' using a viewpager because I'll be adding more fragments in the future.
I assume the reason this is happening is because when C's fragment is updated, A's and B's fragment is having the old instance of the viewpager (or fragment). So, when I come back to A or B, the old data is being shown.
If this is the problem what is the best solution?
I have tried using viewpager.notifyDataSetChanged(),this works but messes up the views in the fragment.
Can anybody suggest an alternative?Thank you in advance!

Android - How to avoid onResume of fragments when popping them from the backstack

We have multiple screens in our app and to switch between them we use FragmentTransactions, primarily replace, and we add fragments to the backstack when we do so. While doing so we stay within the same MainActivity.
So if we transition from Fragment A to Fragment B via some button we now have a stack that looks like
B
A
If we go to another Fragment C it goes on top of the stack like so
C
B
A
However, sometimes we transition to a fragment D, such that we want to wipe out the backstack so that users can no longer navigate back through C, B, A. We now want our backstack after navigating to D to look like this.
D
But in order to do so we need to clear the backstack using:
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
while(supportFragmentManager.getBackStackEntryCount() > 0)
{
supportFragmentManager.popBackStackImmediate();
}
clearingBackStack = false;
But when we do so, the onResume of fragments C,B and A are called. This is not the functionality we intend and can have negative consequences such as making unnecessary server calls. We have also noticed on low end devices that we see a flash of the popped fragments as they come off the stack.
We wish to avoid this behavior, is there a way to pop the entire fragment backstack without the popped fragments being activated in anyway?

Knowing when a fragment is left from / returned to

Android's Fragment's onResume/onPause methods are tightly coupled with the host Activity's lifecycle as shown here.
What I want to know is how to detect that a fragment is left from / returned to inside the app's navigation flow.
Example:
Say I have MainActivity and fragments A,B and C.
MainActivity adds fragment A, then B and then C.
How do I know fragment B was left (I now see fragment C).
Also, once I press on back, how do I know fragment B was resumed?
Edit:
Clarification: I want to know that from within fragment B (similar to the way an Activity works with onPause and onResume)
Try isDetached() method link here
Respectively there is isAdded()
Indirectly you question concern with Fragment LifeCycle.
And you can trace fragment navigation replace,remove,add using Fragment manager
PopBackStack : Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned.

Categories

Resources