I have an activity with a fragment A inside of it. The fragment A have nested fragment B inside of it. I'm switching B with C using following code:
getChildFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.move_left_in, R.anim.move_left_out,R.anim.move_right_in, R.anim.move_right_out)
.replace(R.id.container, fragmentC)
.addToBackStack("nested")
.commit();
after that Im doing
getChildFragmentManager().popBackStack();
what brings me back to fragment B.
After that I switch fragment A with fragment D using code
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.move_up_in, R.anim.move_up_out, R.anim.move_down_in, R.anim.move_down_out)
.replace(R.id.fragment_holder, fragmentD)
.addToBackStack("fragments")
.commit();
and while that animation on A->D transaction is playing, the B plays animation of transaction C->B, and by poping backstack im getting same result, why?
When you switch fragment A with fragment D.
The content within fragment A detach from its parent and then transaction A -> D takes place,
Since you have already set animation to your fragments, delay due to those animation is the reason you,see that left out animation in B first,
then A -> D animation.
Don't use the getChildFragmentManager(),
Use support fragmnetManager means use getSupportFragmentManager why?
Bcz of nested fragment
I hope your problem is solved with this solution. Thank you:)
Related
My App has only one activity and lots of fragments.
In my activty's XML, I just have a FrameLayout on which I replace/add/hide/show various fragments.
Imagine Fragment A is the first fragment the user sees when they open the app.
Click something in Fragment A to launch Fragment B and click something in Fragment B to launch Fragment C.
So the navigation is can be illustrated as follows :
Fragment A --> Fragment B --> Fragment C
I want to launch the app and show Fragment C directly from notification.
However, how can I provide back navigation from Fragment C, as such clicking back would go to Fragment B and clicking back again go to Fragment A ?
i.e How can I inject the following stack structure ?
Fragment A <-- Fragment B <-- Fragment C
Yes, you can do this. Since support library v26 you can build the stack with fragments without significant cost.
In your activity make the following:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentA())
.addToBackStack("fragmentA")
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentB())
.addToBackStack("fragmentB")
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentC())
.addToBackStack("fragmentC")
.setReorderingAllowed(true)
.commit();
Keep in mind that FragmentA and FragmentB will behave in a bit different way after pressing back on FragmentC due to setReorderingAllowed. onCreateView won't be called for FragmentA and FragmentB after they were added to stack, only in FragmentC onCreateView will be called. For FragmentA and FragmentB only onCreate will be called.
What you can do is -
Use a notification intent in which you pass a string. In your main activity if you receive that string make a fragment stack of A, B and C.
Else if you don't get the intent just continue your flow as it is.
I have an app that has a single activity where all the screens are displayed as fragments.
So when most of the times I am changing the fragment I add it to the back stack. So the BackStack can get quite big.
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.leftPane, new ReleaseFragmentBuilder(releaseId).build())
.addToBackStack(null)
.commit();
My question is if I want to replace a fragment but do not keep the BackStack anymore(BackStack Reset) , one option is to clear the BackStack by doing a PopBackStack until root (which is super inefficient and have seen other problems) OR to do a fragment replace WITHOUT adding it to the BackStack
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.leftPane, new HomeFragment())
.commit();
So What do you recommend? If I use only the replace , Does it have any memory issues?
I have an Activity that is holding 5 fragments.
One of those fragments is holding 5 more fragments.
if i add to the fragmentManager a .addToBackStack(null).
the back button returns to the last fragment from the activity and not to the last fragment from the "father" fragment (that is holding 5 more fragments).
Any help please..
EDIT:
ACTIVITY:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
FRAGMENT:
fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().
replace(mainContent.getId(), currentFragment)
.addToBackStack(null)
.commit();
This is probably the bug mentioned here: https://code.google.com/p/android/issues/detail?id=40323
You can workaround it by handling the 'back' manually. Refer to this thread for a lot of workarounds: Android 4.2: back stack behaviour with nested fragments
I think the issue might be with "the fragmentManager" here. There is more than one FragmentManager.
There is the FragmentManager for the Activity – Activity.getFragmentManager().
And there is the FragmentManager for child Fragments within a Fragment – Fragment.getChildFragmentManager() or Fragment.getFragmentManager().
Let's say you have an activity, a parentFragment and a childFragment. So, instead of activity.getFragmentManager(), I think in your case, you might want either childFragment.getFragmentManager() or parentFragment.getChildFragmentManager().
Note that if you are using the Support package, the names may be different.
I've been working with fragments for a while now, but I regularly encounter a problem that just annoys me. Fragments remain drawn over each other some times. Now, I managed to isolate one use case for this, and it goes like this:
Add Fragment A (also use addToBackStack with a name "backstack_state")
Replace Fragment A with Fragment B (use addToBackStack)
Replace Fragment B with Fragment C WITHOUT using addToBackStack
at a given point use popBackStack("backstack_state", 0) and here comes the issue:
The backstack is popped until Fragment A but Fragment C is overlaid with Fragment A, both are visible at the same time. Is this normal behavior or is it me who makes a mistake?
Here's a remark also: all the fragments have transparent background.
Thanks!
This happens because the top fragment (in this case Fragment C) is not removed. You have to remove it first inside a fragment transaction. Try this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment topFragment = fragmentManager.findFragmentById(R.id.fragment_container);
if (topFragment != null) {
fragmentTransaction.remove(topFragment);
}
fragmentTransaction.commit();
fragmentManager.popBackStack("backstack_state", 0);
I have a app they use Android.Support.V4.App.Fragment. Is it possible to launch a new Fragment from a "parent" Fragment?
For example Fragment A switch to Fragment B and when you press back from the Fragment B it change for the Fragment A.
Thanks.
This code will help you.
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.your_tab_id, new FragmentClassName())
.commitAllowingStateLoss();