According to I have an Activity which contains ViewPager which loads three fragments:
RootA
RootB
RootC
RootA loads another fragment which named as FragA.
RootB loads another fragment which named as FragB.
RootC loads another fragment which named as FragC.
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.container, new FragA());
fragTransaction.commit();
FragA has a button named as ButtonA to load FragAA.
FragB has a button named as ButtonB to load FragBB.
FragC has a button named as ButtonC to load FragCC.
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
fragTransaction.replace(R.id.container, new FragAA, "FragAA");
fragTransaction.addToBackStack("FragAA");
fragTransaction.commit();
I run my App and in the first tab, I press ButtonA. FragAA will be shown. Now I swipe to the second tab and press ButtonB and go to FragBB and then I swipe to the third tab and press ButtonC and go to FragCC.
After that, I swipe to the second tab and then I swipe to the first tab again without any clicking. then I press Back Button. By the first back pressing FragCC will be closed (will put out of BackStack), by the second back pressing FragBB will be closed, and by the third back pressing FragAA will be closed.
But I want my app to close child of current tab not to close fragments according to the BackStack.
I mean the Back Button should close FragAA because I am in the first tab and I pressed Back Button in the first tab!
How can I do it?
Related
In My application I have 5 tabs.
In 1st tab when user clicks a button I need to go to 2nd tab which contain 3 fragments.
1) List Fragment
2) Details Fragment and
3) Result Fragment
When user click on a button I want to go directly to the Details Fragment, and when clicks back it should go to the list.
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
In your code instead of calling getActivity().getSupportFragmentManager() call getActivity().getchildfragmentmanager()
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getChildFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
I am working on android app and use fragment for displaying user interface.
I am different fragment,i have two problem in app-
1. I am facing problem when user navigate from one fragment to another then multiple instance of same fragment is added in backstack.
Suppose user move from fragment1 to fragment2 then fragment3 and again move to fragment2 and thenpress back button then from backstack fragment3 is load and then fragment2 and then fragment1.so i just want to add a fragment one only in backstack.
I am creating only one instance of a fragment bu using my custom method for each fragment like-
public static Fragment getInstance(FragmentManager f){
if(mFragment==null){
mFragment = new HomeFragment();
}
fm = f;
return mFragment;
}
so each time when a fragment is replaced with a previous instance of same fragment.
2 after navigate to different screens when user presses virtual android mobile home button and app goes to sleep and after few hours if user again opens app then app force close.
Thanks in advance
so heres the issues,
i'm using fragmenttabhost with four tab, Tab A(FragA), Tab B(FragB), Tab C, Tab D
when i using Tab B, and i do a fragment transaction to replace the fragment on Tab B(FragB) to another fragment(FragB_2), it is added to the back stack, so i can still navigate back by using the back button. but when at Tab B new fragment, i click on Tab A, Tab C, Tab D. then when i go back to Tab B, the tab will be displaying the new fragment(FragB_2), and when i press on the back button.. no fragment is display.
i had try to remove the backstack whenever i do tab change. etc
if (fm.getBackStackEntryCount() > 0)
{
Log.i("MainActivity", "popping backstack");
fm.popBackStack();
}
this code remove the Fragment B_2 from the TabB, but whenever i go back to Tab B again, Fragment B is gone.
how to i go around it?
I have a form divided in few fragments. I call every fragment with:
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
if(fragment.equals(this.formOne) || fragment.equals(this.formTwo)) {
ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
ft.replace(R.id.fragForm, fragment);
ft.addToBackStack(null);
ft.commit();
When I click on the back button it skips the previous Fragment and goes back to the Activity but with a blank screen.
For example, I have 3 fragments : A - B - C
If I go to C and want to go back to the previous Activity, I click on the back button so I'm on B, I click again and I'm on A, and when I click again, I have a blank screen, I need to click another time to come back to the previous activity.
I don't understand why have I this blank screen on my Activity.
I don't understand this in the developer documentation:
Note: You should not add transactions to the back stack when the
transaction is for horizontal navigation (such as when switching tabs)
or when modifying the content appearance (such as when adjusting
filters). For more information, about when Back navigation is
appropriate, see the Navigation design guide.
If we can't use that, what is the solution?
Don't add it to the backstack when you add fragment A.
ft.add(R.id.fragForm, fragmentA);
ft.commit();
ft.replace(R.id.fragForm, fragmentB);
ft.addToBackStack(null);
ft.commit();
ft.replace(R.id.fragForm, fragmentC);
ft.addToBackStack(null);
ft.commit();
If you don't have to Navigate to previous fragment don't add it to the backstack
Remove this
ft.addToBackStack(null);
Activity 1 is visible. Press a button, and Activity 2 opens.
Activity 2 adds fragment A to itself (and back stack) and it displays fine
Pressing a button within the fragment transitions to another fragment, B
Press Back. Nothing happens. Huh? The Back press is seemingly absorbed and not acted upon, the display remains the same.
Press Back a second time, it reverts to the Activity 1, as expected.
Why is my fragment not being shown in step 4? I've added the fragment to the back stack, so why (when the Back button seems aware of its existence) does it not show the fragment?
Here's the code I'm using in Activity 2 to open Fragment A.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_profile_edit);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.addToBackStack(null);
transaction.add(android.R.id.content, new MyFragment());
transaction.commit();
}
And here's the code to open Fragment B
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.add(android.R.id.content, new MyOtherFragment());
transaction.commit();
Have you tried transaction.replace(...) instead of transaction.add(...)? That should work. I'm guessing because if you're just adding a fragment over another, it doesn't see transaction as wanting to go back fro Fragment A.
EDIT
The actual answer for the question is below in the comments: addToBackStack() should be used on the fragment which is replacing, not the one being replaced.