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?
Related
The project is multiple-fragment project. Launch application to start BaseActivity, which all fragments are attached into it.
Issue:
Fragment A is the first fragment and is launched very well.
Fragment B is trigged by clicking button of Fragment A, also launched very well.
During from B back to A, the problem occurs. Nothing to display, only background, the BaseActivity.
All fragments use the api "replace" to display, although this API may produce performance problems, please ignore it firstly.
I have tried many methods, but nothing works.
The launched fragment method shows below:
public void navigateToFragment(Fragment fragment, boolean isClear){
if(isClear){
getSupportFragmentManager()
.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.container,fragment)
.commit()
}
As I know, popBackStack and replace, commit can lead to one fragment is launched twice, but why no display after replace whatever I replace it?
try use add instead of replace
getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.add(R.id.container,fragment)
.commit()
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:)
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 want to know
getFragmentManager()
.beginTransaction()
.replace(
R.id.main,
Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment1",
bundle)).commit();
and then later on we call
getFragmentManager()
.beginTransaction()
.replace(
R.id.main,
Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment2",
bundle)).commit();
What happens to the Fragment1 view ? Will it be destroyed automatically, do we have to manage any garbage collection on this ?
Kind Regards
According to the note on Android Developers:
When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.
When click the items of NavigationDrawer,The main activity container switch the fragments to show,It worked fine until I met this:
1.Switched to FragmentA, which CONTAINS A VIEWPAGER,it showed well.
2.Switched to fragmentB,fragmentB showed well.
3.Swiched back to
FragmentA,it shows as a Blank View
I tried to flip horizontally on it,I can see the viewpager index do changed(in log),But I don't know why it showed as a blank page.
*and,if fragmentA dose not contains viewpager,it worked well
Any suggestion would be appreciated.
I use replace() to switch between the fragments:
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, currentFragment)
.commit();
Possible reason might be not using replace transaction instead you mightbe adding fragments and adding it to backstack. Try using replace transaction, and see if it works.
Fragment1 firstFragment = new Fragment1();
Bundle bundle = new Bundle();
firstFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.commit();
Learn More on Fragments adding to back stack:
Android Fragment transaction: FragmentManager and Backstack