Android Navigation between fragments - android

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

Related

Why does not `replace` replace a fragment?

I have the following code that adds fragments to the same container R.id.container. First I add one fragment, then after user interacts with it, I add another one with the same code
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
My question when I cell getSupportFragmentManager().getFragments() I can see two fragments. Why doesn't replace actually replace but add?
It keeps the two fragments because
addToBackStack(null);
means the user is able to revert back to the previous state.
If you add fragment1 (using replace), then fragment2, only fragment2 is shown, but if the user press back, the FragmentManager needs to show fragment1, so it keeps a reference to both fragment.

Android: Fragments act unexpected (suddenly transparent?)

I have a problem with my fragments. I use this code to navigate between the fragments:
Between the "main" fragments (without backstack, because I want the user to exit when he presses back (it works)):
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
ft.replace(R.id.container, NewsFragment.newInstance(position + 1), NewsFragment.class.getSimpleName());
ft.commit();
and between the "inner" fragments (with backstack):
FragmentTransaction ft = ((Activity) getActivity()).getFragmentManager().beginTransaction();
Fragment nextFragment = LexikonDetailFragment
.newInstance(item);
ft.setCustomAnimations(R.anim.slide_in_up_honeycomb, R.anim.slide_out_up_honeycomb);
ft.replace(R.id.container, nextFragment);
ft.addToBackStack(LexikonDetailFragment.class.getSimpleName());
ft.commit();
But in the following case:
fragment A -> fragment A1
fragment A1 -> fragment B
Press back button (should end the app)
-> goes back to fragment A1
happens:
Image
It looks like the A1 fragment doesnt gets removed from the backstack and stays in the background. I thought one possible solution could be to set to all fragments a white background..but this wouldn't fix the problem, it would just hide it. So what could be a possible solution?
Okay, i found a solution:
I had to remove the inner fragments manualy via the command:
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
I call this command each time my "outer" fragment changes. The inner ones get removed and tada...it works like a charm :)

Fragment popBackStack without animation

On navigating from one fragment to another which are part of same activity, I am using slide animation.
WebFragment fragment = WebFragment.newInstance(Globals.TGURL_CREATE_ACTIVITY, "");
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.enter_anim, R.anim.exit_anim);
transaction.replace(R.id.fragment_activity_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
This code ensures that when I come back to the first fragment, animation is there.
Well this has been a boon for me so far. But in one specific case it is becoming a curse. The activity is placed on ActionBar tab. When the 2nd (WebFragment) is the current one, and I tap the tab and not the back button, I want the first fragment to be displayed without any animation.
But it has been impossible for me to do this so far as the navigation is inheriting the animation which was earlier given.
This is what I am doing for going back :
TabActivity.this.getSupportFragmentManager().popBackStack();

Android Fragments - How to implement Backstacking and Recalling previous fragment

I have android screen layout shown below
Apps screen divided into 3 fragments, Header, Footer and Content. Header and Footer fragments are fixed. Content fragment is changing according to content. I replaces fragment1-fragment3 according to need. Initially Fragment1 is shows in content area. When i press a next button fragment1 is replace by fragment2. This is the order. I have question, if i press another button previous, how can i return to previous fragment ( fragment2 -> fragment1). Is any built in mechanism available in fragment class.
Please guide me...
thanks in advance
try this code
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
Whenever you try to call new fragment then add back stack for it through
ft.addToBackStack(null);
And now if you want to back from one fragment to another then, make one method..
public void DeleteCurrentFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.detailFragment);
String fragName = "NONE";
if (currentFrag!=null)
fragName = currentFrag.getClass().getSimpleName();
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
And after that call this:
getSupportFragmentManager().popBackStack();
DeleteCurrentFragment();
you can try something like:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.contentFragment, <your fragment>);
ft.addToBackStack(null);
ft.commit();
so suppose you're on fragment1 and after doing some stuff you want push fragment2 then on your contentFragment you can add or replace the fragment (fragment2 in this case) as per your requirement. Now when you call addToBackStack it means add this transaction to the back stack. So that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack. Internally it maintains a stack so you don't want to do anything. At last when you press back button from fragment2 it checks whether the transaction having any fragment in its stack, if yes then it calls it. Its like top of stack. When you call addToBackStack in that stack fragment1 is added. So when you press back button stack's top is fragment1 so it calls it.
It happens when you press hardware back button. If you want to do this on any button, then on that button's click listener simply onBackPressed() method.

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