1

Im combining replace with addtobackstack for new fragments added the activity. like so:

mFragmentManager.beginTransaction().replace(R.id.content_frame, searchFragment,"Frag_Search").addToBackStack("searchFragment").commit();

When the user back presses twice, i would like the app to exit

I tried some answers here: Clicking the back button twice to exit an activity but the don't close the app, just keeps removing the last fragment.

Community
  • 1
  • 1
raklos
  • 28,027
  • 60
  • 183
  • 301

3 Answers3

0

You can exit from your fragment by calling getActivity().finish()

0

You may have to empty the backstack first by doing something like this

private void popEveryFragment() {
    // Clear all back stack.
    int backStackCount = mFragmentManager.getBackStackEntryCount();
    for (int i = 0; i < backStackCount; i++) {

        // Get the back stack fragment id.
        int backStackId = mFragmentManager.getBackStackEntryAt(i).getId();

        mFragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    }
}
esser
  • 43
  • 4
0

Please, try to set the code of the top answer from this question Clicking the back button twice to exit an activity inside your main activity.

Community
  • 1
  • 1
Mahmoud Ibrahim
  • 1,057
  • 11
  • 20