I have coded like this:
FragA >> FragB >> FragC >> FragD
When I press onBackpress() of FragD it goes to directly on FragA but i want to keep it go FragC.
BackPress Code here :
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0) {
back_btn.setVisibility(View.GONE);
getActivity().getSupportFragmentManager().popBackStack();
}
Replacing Fragment Code :
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentA = new FragmentA();
fragmentTransaction.replace(R.id.framelayoutinner, fragmentA);
fragmentTransaction.addToBackStack("fragmentA");
fragmentTransaction.commit();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentB = new Fragmentb();
fragmentTransaction.replace(R.id.framelayoutinner_2, fragmentB);
fragmentTransaction.addToBackStack("fragmentB");
fragmentTransaction.commit();
You are using nested fragments, if you load fragment from another fragment then it become nested structure. So try to check if any child fragment exist within your Root fragment then pop that child fragment first.
Use getChildFragmentManager() for that and make recursive calls till you get most young child fragment(Last added). Or better use Tag on your fragments.
You need to add every fragment into backstack using addToBackStack method
public void setmFragmentContainer(Fragment fragment)
{
final String tag = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout_content, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
This method may help you
Related
I have three fragments (fragmentA, fragmentB, FragmentC). The code goes from fragmentA to fragmentB and then fragmentB to Fragemnt C.
When I press the back button I go from fragmentC to fragmentA.
After the back button is pressed fragmentA is displayed but you can also see fragmentC behind it.
fragmentA
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentB)
.addToBackStack("Null")
.commit();
fragmentB
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentC)
.commit();
fragmentC
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentC)
.commit();
When the back button is pressed I want to go from fragmentC to fragmentA and not have fragment C displayed in the background
In addToBackStack use null as param , not "Null"
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction;
ft.repalce(R.id.container, fragmentB)
.addToBackStack(null)
.commit();
you can try this when replacing fragment
Fragment frag=null;
frag=new Navigation_Help();
if(frag!=null){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
ft.replace(R.id.sacreenarea,frag);
//for not to back previous fragment remove next nile
ft.addToBackStack(null);
ft.commit();
}
I tried using #ADM duplicate link, it would take me to the fragment A but if I loaded fragment B for a second time I would get double commit errors.
The solution that works for me is to restart the activity with the #Override onBackPressed(), this will reload the fragmentA by default.
Fragment B
FragmentManger fm = fragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fm.replace(R.id.container, fragmentC, "fragmentC");
.commit();
fragmentC backbutton pressed
Activity
#Override
public void onBackPressed() {
if(getSupportFragmnetManager().findFragmnetByTag("fragmentC") !=null) {
Intent myIntent = new Intent(this, Activity.class);
startActivity(myIntent);
finish();
} else {
super.onBackPressed();
}
}
This solution will only work if you want to go back to the first fragment loaded by the Activity, so it may not be the best solution out there but it worked for me.
I have implemented fragments in my application. Here my code for swiching fragment in fragment_container.
private void switchFragment(Fragment fragment, boolean isAddToBackStack, String tag)
{
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment, tag);
if (isAddToBackStack)
ft.addToBackStack(tag);
setCurrentTopFragment(Integer.parseInt(tag));
ft.commit();
}
I have 4 fragments A,B,C and D and for switching between this fragmnets I am using above method. I have A,C,B in my backstack. If again I switch to fragmnet A, my backstack is like A,C,B,A. What I actually want is If I swich to A again I want backstack sequence like this C,B,A. Means Remove old instance from backstack and add new to it.
First get the back-stacked Fragment by id which you need to remove:
Fragment fragment = getSupportFragmentManager().getFragment(new Bundle(), TAG_KEY)
or there are several methods getBackStackEntryCount(), getBackStackEntryAt. After getting the fragment which you need to remove. Remove it from the fragment back-stack.
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
Then you can add a new fragment Done :)
I have a fragment in which there is a nested fragment which I add in this way:
if (home == null) {
home = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(MyFragment.class.getName());
transaction.add(R.id.child_fragment, home).commit();
}
When I enter another fragment and go back the child fragment from above is not there. I checked and the instance is different from null.
UPDATE: I changed the code as suggested by Ashwin S Ashok but it's still not working.
Try using these methods:
// Use this if you don't want to retain the fragment.
protected void replaceFragmentStack(int container, Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(container, fragment);
fragmentTransaction.commit();
}
// Use this if you want to add the fragments in a stack.
protected void addFragmentStack(int container, Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
I would suggest you to use getChildFragmentManager() when making transactions inside a fragment. And its a bug i guess.
You can check out this thread it will help you alot Android 4.2: back stack behaviour with nested fragments
Also you need to go through The Curious Techizen's blog
Here is the link for the github project sample for same mechanism
I hope this will help you.
I'm trying to implement the following fragment design:
Fragment A is replaced by fragment B, which in turn, is replaced by fragment C.
Whether in fragment B or C, I want the user back navigation to take it back to fragment A.
I add to backstack when replacing A with B. But when I move from B to C, I don't add to backstack.
When navigating back from fragment B, everything works fine.
But, when navigating back from C, I get A and C on the same screen - C doesn't disappear.
I wonder if it is related to my backstack usage.
Any help is appreciated.
My code is equivalent to:
Fragment fragment;
fragment = new FragmentA();
transaction.replace(R.id.container, fragment);
transaction.commit();
fragment = new FragmentB();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
fragment = new FragmentC();
transaction.replace(R.id.container, fragment);
transaction.commit();
This is the general way to add to Backstack. Use tags.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.fragment_container, fragment1, Fragment1.class.getName());
fragmentTransaction.addToBackStack(Fragment1.class.getName());
fragmentTransaction.commit();
Now similarly, for frament2:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2, Fragment2.class.getName());
fragmentTransaction.addToBackStack(Fragment2.class.getName());
fragmentTransaction.commit();
Do the same for fragment3.
And now you remove fragment2 from backstack using:
getFragmentManager().popBackStack(
Fragment2.class.getName(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
You should be able to skip directly from fragment3 back to fragment2.
Also, are you using fragmentTransaction.replace(...) and not fragmentTransaction.add(...). Could you post relevant code?
I am developing a simple android app only for tablets and using android 4.0. My application have the main screen like as follow:
Oncreate() of Main Activity I am adding Fragment A in the main.xml using following code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment imageFragment = new ImageFragment();
ft.replace(R.id.fragment_container, imageFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
This fragment A just have only a Image view which is clickable. Now I want that when user click on Image view then another fragment (Fragment B) should call and it replace the image view. The Fragment B have a VideoView which play the video.
So My second screen should be like as follow:
My problem is I am not gettting "How to call second fragment from the first one with in main screen activity?"
I can use different activities but I do not want to do so and just want to run this using fragments.
Please guide me.
This is the simplest way:
1) Inside YourActivitycreate a method:
public void goToSecondFragment(){}
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment secondFragment = new SecondFragment();
ft.replace(R.id.fragment_container, secondFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
2) In your first fragment, when you want to replace it, call:
YourActivity activity = (YourActivity) getActivity();
activity.goToSecondFragment();
You can do the same using below:
Adding a fragment with maintaining a back stack
FragmentManager supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
}
});
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("fragmentTag");
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
And Replacing a fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
Should work for you.