Issues navigating back with NavigationDrawer - android

I'm using the Android Studio template for Navigation Drawer, but I just can't figure it out how to properly navigate back from the new Fragments I added to the ones already existing.
I have seen many posts about similar problems, but couldn't fix it with the answers.
Fragment manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
And my many tentatives:
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment);
transaction.addToBackStack(null);
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment);
transaction.addToBackStack("GalleryFragment");
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment, "GalleryFragment");
transaction.addToBackStack(null);
Gets back to an empty screen
transaction.replace(R.id.fl_nav_host_content_main, entitiesFragment, "GalleryFragment");
transaction.addToBackStack("GalleryFragment");
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment);
transaction.addToBackStack(null);
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment);
transaction.addToBackStack("GalleryFragment");
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment, "GalleryFragment");
transaction.addToBackStack(null);
The new Fragment design shows incorrectly. At first does not navigate back; at second does navigate back twice
transaction.replace(android.R.id.content, entitiesFragment, "GalleryFragment");
transaction.addToBackStack("GalleryFragment");
Can someone help?
Thanks in advance.

Related

Bottom navigation using android support library and fragment

I have downloaded https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/ this code.After running the bottom tab is working fine,but when i am clicking on android back button that time the fragment is coming one step back,but the bottom navigation item is not coming as per fragment.
Suppose i have clicked "cart" and the fragment shows "My Cart" ,After this when i am clicking android back button ,the fragment is showing "My Gifts" but bottom navigation is still on "Cart".I want bottom tab should also come one step back.But I am not getting what is the problem here.Can anyone please help me out with this.
You can do one change. remove "transaction.addToBackStack(null);"
Why
FragmentTransaction addToBackStack (String name)
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
https://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
}

Leaking Fragments from FragmentManager

I'm struggling with a Fragment leak and I am not sure how to handle it:
I have a FragmentActivity(I'm using support lib v4) with two buttons: forth and back. Forth button adds a fragment to the backstack with a long animation and back button pops the fragment from the backstack.
Code for forth button click listener:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.own_slide_in_left, R.anim.own_slide_out_right, R.anim.own_slide_in_left, R.anim.own_slide_out_right);
transaction.add(R.id.fragment_holder, new FirstFragment());
transaction.addToBackStack(null);
transaction.commit();
And for back button click listener:
getSupportFragmentManager().popBackStackImmediate();
When I press the back button and rotate the device while the animation is ongoing then after rotation visually everything seems to be ok, but the previous Fragment is leaked. I can tell it by having a breakpoint in, for example, Fragment's ctor or onSaveInstanceState(). It will be it on each rotation when it shouldn't be.
What am I doing wrong?
EDIT: This problem persists using native Fragments, Android 4.0.3

Android Navigation between fragments

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

Android Fragment Unable to go back

I have an app that uses fragments with tabs/viewpager
[Tab 1][Tab 2][Tab 3]
Tab2 has a ListView and in the onClick method of the ListView I am showing a detail view with the following code
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
NextFragment nextFragment = new NextFragment();
transaction.replace(R.id.container, nextFragment);
transaction.addToBackStack(null);
transaction.commit();
The issue is that in the NextFragment when I use the hardware back button the whole app closes?
I'm not sure why it doesn't let you return when back is pressed, but what I've done in my application is the following:
I have overriden the onBackPressed action and created a check that once it is pressed and the user is in the fragment that he wants to return from, just create another fragment transaction to the previous fragment.
I have done so because I needed to update the previous fragment, but I'm sure there is a better way to solve your problem.

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