Two fragments are displayed when clicking back - android

I have a main container in xml file in which i will replace the fragments depending on the button clicks.
when Activity opened i am adding fragment1 to the main container with the following code
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, homeMainFragment);
fragmentTransaction.commit();
in HomeMainFragment on click of one button doing the following.
Fragment2 fragment2 = new Fragment2 ();
fragmentTranasaction.replace(R.id.container, fragment2);
fragmentTranasaction.addToBackStack(null);
fragmentTranasaction.commit();
in Fragment2 on click of another button moving to Fragment3 using the following code.
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment3 fragment3 = new Fragment3 ();
transaction.replace(R.id.container, fragment3 );
transaction.commit();
now when i click back my UI should show HomeFragment instead it showing home as well as fragment3 merged together.
Just wanted to clear on my requirement.
1.when I am viewing fragment3 and click on back i should actually see the homefragment not fragment2.
2. I am not adding fragment3 to the backstack because one of the button in fragment3 again starts fragment2 which will start fragment3. (same code shown above is used) in this case if I click on back I can see multiple instances of fragment3 which i don't need. Please help to solve this issue.
Edit 2:
These are my observations.
in step 1: container has homefragment.xml
in step 2: container replaced by fragment1..xml
in step 3: container is replaced by fragment3.xml
when i click on back container is being added with homefragment which is of transaparent background thats why fragment 3.is still visible.
is there any way when user clicks back remove the backstack and add homefragment alone?

I think this is what you have to do.
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, homeMainFragment);
fragmentTransaction.commit();
in HomeMainFragment on click of one button doing the following.
Fragment2 fragment2 = new Fragment2 ();
fragmentTranasaction.replace(R.id.container, fragment2);
fragmentTranasaction.addToBackStack().commit();
in Fragment2 on click of another button moving to Fragment3 using the following code.
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment3 fragment3 = new Fragment3 ();
transaction.replace(R.id.container, fragment3 );
transaction..addToBackStack().commit();
Refer below link for more info.
http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
According to the documentation,
When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).
Hope this helps.

You can try:
getFragmentManager().popBackStack();

Related

how to save api data in fragment back stack from fragment two to fragment one on back button clicked

i have two fragments,in first fragment i have list of invoice details with check box in list view .
i need to store the list items while back from the second fragment
1st fragment:
MakePayment debtorsAging = MakePayment.newInstance();
debtorsAging.setArguments(bundle);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, debtorsAging,MakePayment.TAG);
fragmentTransaction.addToBackStack(PaymentCollection.class.getName());
fragmentTransaction.commit();
2nd fragment:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
ft.remove(new MakePayment());
ft.commit();
fragmentManager.popBackStackImmediate();
need to restore list of invoices with checked items on go back from makePayment fragment to paymentCollection fragment
If you back to previous activity by onBackPressed (not by transaction) i think you cant pass anythig between fragments by bundle or something. One of resolution I suggest to save id's or something to SharedPreferences before back to previous activity and restore them in first fragment

Fragment Save State

I've read a lot about save states and fragments, but I can't wrap my head around it.
I have a fragment that is loaded when the user signs in. The user can then choose from a variety of categories, which will replace the current fragment with a new one. In this new fragment, the user can click items in a GridView which will cause a check to appear.
When I press the back button, how would I go about saving the clicked items? onSavedInstanceState is never called, and it seems like my fragment is being created all over again when I try to navigate back.
Some code:
When the user first enters the app
// creates the home fragment
Fragment homeFragment = new HomeFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.content_frame, homeFragment);
fragmentTransaction.commit();
When the user clicks on a category
Bundle bundle = new Bundle();
bundle.putSerializable("subCategory", subCategory);
Fragment ingredientsFragment = new IngredientsFragment();
ingredientsFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, ingredientsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
When you move From HomeFragment To IngredientsFragment you are replacing 1 fragment by another (HomeFragment By IngredientsFragment) so when you press back from IngredientsFragment then It will remove IngredientsFragment from stack also and HomeFragment is already removed and this is expected behavior.
What you can do is When you invoke IngredientsFragment Fragment then "add" it not replace and when user press back then remove IngredientsFragment from stack.

How to delete a specific fragment from back stack in android

I have a problem about removing a specific fragment from back stack.My scenario is like this.Fragment-1 is replaced with Fragment-2 and then Fragment-2 is replaced with Fragment-3.
Calling order; Fragment-1-->Fragment-2-->Fragment-3.
When Fragment-3 is on the screen and then back button is clicked, i want to go
Fragment-1.That means i want to delete Fragment-2 from back stack.
How to do this ?
In the backstack you don't have Fragments, but FragmentTransactions. When you popBackStack() the transaction is applied again, but backward. This means that (assuming you addToBackStackTrace(null) every time) in your backstack you have
1->2
2->3
If you don't add the second transaction to the backstack the result is that your backstack is just
1->2
and so pressing the back button will cause the execution of 2->1, which leads to an error due to the fragment 2 not being there (you are on fragment 3).
The easiest solution is to pop the backstack before going from 2 to 3
//from fragment-2:
getFragmentManager().popBackStack();
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment3)
.addToBackStack(null)
.commit();
What I'm doing here is these: from fragment 2 I go back to fragment 1 and then straight to fragment 3. This way the back button will bring me again from 3 to 1.
I had a very similar scenario to yours, my solution was just checking the amount of backStack transactions that I had.
If the transactions where more than 0 then I would simply pop it right away
so it would skip it when pressing back.
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate();
}
...
fragmentTransaction.replace(R.id.main_fragment, newFrag, MAIN_FRAGMENT_TAG);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
This would successfully:
A -> B (back pressed) -> back to A
A -> B -> C (back pressed) -> back to A
The only downside that I see is that there is a quick flash where fragment A is displayed before going to fragment C.
If you are adding/launching all three fragments in the same activity, instead of the add(), use replace() (replace Fragment2 with Fragment3). The replace method removes the current fragment from backstack before adding the new one. If you are launching Fragment3 from a different activity, and thus you can't use replace(), remove Fragment2 from backstack before starting the new activity (which adds Fragment3):
// in Fragment2, before adding Fragment3:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.remove(this) // "this" refers to current instance of Fragment2
.commit();
fragmentManager.popBackStack();
// now go ahead and launch (add) fragment3
// if fragment3 is launched from a different activity,
// start that activity instead
fragmentManager.beginTransaction()
.add(R.id.a_container_view_in_activity, new Fragment3(),
Fargment3.FRAGMENT3_ID)
.commit();
Code for Fragment A -> Fragment B:
Add Fragment A in BackStack of Fragments
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_container, new fragmentB());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Code for Fragment B -> Fragment C:
Do not Add Fragment B in BackStack of Fragments
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_container, new fragmentC());
fragmentTransaction.commit();
It will works in this way: A -> B -> C and while returning C-> A as you excepted.
Hope it will help you.
for (int i = 0; i < mFragmentManager.getBackStackEntryCount(); i++) {
currentFragment = mFragmentManager.getFragments().get(i);
if (!(currentFragment instanceof ParticularFragment))
mFragmentManager.popBackStack();
else
break;
}

non trivial android fragments backstack

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?

how to call fragment having no layout

I have 2 fragments FileManage and another is FragmentA. FileManage has no layout of it's own but FileManage class wants to call FragmentA .so how can be This Possible . i have tried code given below but it is not working
FragmentA f12 =new FragmentA();
android.support.v4.app.FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(0, f12);
transaction.commit();
Normally here in replace function we put 1st Paramtere as layout of base Activity ..but here i have no layout so what to do???

Categories

Resources