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
Related
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)
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!
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?
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.
Hi I am developing android application in which I am using fragments and push to back stack. So my scenario is like this I have fragment A, B, C, D and I push them in following sequence A-->B-->C-->D and what I want I want to remove D and C and bring B on top. Is there any way to remove all together and bring one particular fragment on top of the stack. like pop from back stack upto xyz fragment tag. Is there any way to do this. Need help thank you.
You want this: FragmentManager.popBackStack(int id, int flags)
You need to save the value returned by FragmentTransaction.commit() of the transaction that adds Fragment B. Then later call getFragmentManager().popBackStack(commitId, 0);