Dealing with fragmenttransaction when using FragmentTabHost - android

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?

Related

How to handle Back Button in nested fragments of Viewpager

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?

Open inner fragment on clicking a button

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();

Navigation issues in fragment backstack

Multiple taps on bottom navigation bar does not happens as expected
bottom bar has 5 tabs
every tab selection pops the tag and pushes the tag
tap on Fragment A--> tap on Fragment B, repeat the same to two to five times.
ex:
Tap A
--> Pops A from stack
--> push A to Stack
Tap B
--> pop B from Stack
--> push B to stack
behaviour:
by default A fragment is loaded, on tap of B navigating to B, later tap on A goes to A, here when back presses app closed.
expected :
result should be something like this
by default A fragment is loaded, on tap of B navigating to B, later tap on A goes to A, here when back pressed it should go to B and backpress goto A then backpress close the app.
case FragmentA:
fragmenta = new FragmentA();
getSupportFragmentManager().popBackStack(FragmentReferenceTags.FragmentA, FragmentManager.POP_BACK_STACK_INCLUSIVE); // returns False - can't find the tag!
transaction.add(R.id.fragment_container_layout, fragmenta, FragmentReferenceTags.FragmentA);
transaction.addToBackStack(FragmentReferenceTags.FragmentA);
transaction.commit();
hideSubTabs();
break;
case FragmentB:
fragmentb = new FragmentB();
getSupportFragmentManager().popBackStack(FragmentReferenceTags.FragmentB, FragmentManager.POP_BACK_STACK_INCLUSIVE); // returns False - can't find the tag!
transaction.add(R.id.fragment_container_layout, fragmentb, FragmentReferenceTags.FragmentB);
transaction.addToBackStack(FragmentReferenceTags.FragmentB);
transaction.commit();
hideSubTabs();
break;
Use replace instead of add :
transaction.replace(R.id.fragment_container_layout, fragmentb, FragmentReferenceTags.FragmentB);
transaction.addToBackStack(null);

Fragments navigation

I need to create navigation of fragments like in Gmail app. It's like: we have one main fragment A, we can open another fragment (B,C,D...) from navigation drawer, and when we open new fragment, it`s open on top of main fragment, and when press back button, in all cases we come back to main fragment A, don't depend of count new opened fragments. It's seems, first main fragment A we use add method(int FragmentTransaction) without adding to fragment backStack. Then, next fragment B we use method add too, with adding to back stack. And when I need to open another one (Fragment C), I need to replace second fragment B. But, when I use method replace(), replaced all container, and main fragment A not showing when back button pressed from fragment C or B and app close. So, the question is: how to replace only fragment B or C, without losing fragment A?
A valid solution would be to have two container framelayouts in your activity. The first one (which will below the other one) contains your fragment A. Everything you open will be added/replaced in the second container.
Another solution is to include the fragment A statically in your layout and have your container framelayout on top of it where you add your fragments B, C, D etc.
open fragment Like this
HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this); //Your fragment
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, highlightFragment) // LL_fragment is container
.addToBackStack(null)
.commit();
and in Activity OnBackPress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Why is Fragment.addToBackStack() causing the Back button to do nothing?

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.

Categories

Resources