Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Return to previous fragment on back press

I have implemented Navigation Drawer which is a subclass of Activity. I have many fragments in my application. My question goes here

Imagine there are 3 fragments :

Fragment_1 : Fragment_2 : Fragment_3

When I start my application, Fragment_1 is loaded When I click on some components on Fragment_1, I'm navigated to Fragment_2 and so on..

So it's like

Fragment_1 > Fragment_2 > Fragment_3

When I press back key from Fragment_2, I'm navigated back to Fragment_1 But when I press back key from Fragment_3, I'm navigated back to Fragment_1 (instead of Fragment_2)

I want something like this in my application on Back Key press

Fragment_1 < Fragment_2 < Fragment_3

I have used Fragment, FragmentManager, FragmentTransaction as follows :

MyFragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null)commit();

and I tried overriding onBackPressed() in my MainActivity :

@Override
public void onBackPressed() {


        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        int count = getFragmentManager().getBackStackEntryCount();
        if (count == 0)
               super.onBackPressed();
    }
like image 894
Harsh Avatar asked Aug 17 '14 15:08

Harsh


People also ask

How do you ensure that the user can return to the previous fragment by pressing the back button?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

Does popBackStack destroy fragment?

This means that popBackStack() is a destructive operation: any added fragment will have its state destroyed when that transaction is popped.

What is FragmentManager in Android?

A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.


2 Answers

Update your Activity#onBackPressed() method to:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

The reason your implementation doesn't work is because the method FragmentManager#popBackStack() is asynchronous and does not happen right after it is called.

From the documentation:

This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

Reference: http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack(java.lang.String,%20int)

like image 166
Leandro Avatar answered Oct 04 '22 10:10

Leandro


You have to implement your own backstack implementation as explained here

Separate Back Stack for each tab in Android using Fragments

You can call the popFragments() whenever you click the back button in a fragment and call pushFragments() whenever you navigate from one Fragment to other.

in Short,

public void onBackPressed()
{
    FragmentManager fm = getActivity().getSupportFragmentManager();
    fm.popBackStack();
}
like image 22
Sam Avatar answered Oct 04 '22 12:10

Sam