1

Ok i have an activity with one main fragment, that has a menu on it. When a user clicks on a menu item another fragment is animated into the screen, with this code:

FragmentTransaction ft = getFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
ft.hide(getFragmentManager().findFragmentByTag("menu_fragment"));

Fragment opisFragment = getFragmentManager().findFragmentByTag("opis_fragment");
if (opisFragment == null) {
    opisFragment = new OpisFragment();
    ft.add(R.id.p_container, opisFragment, "opis_fragment");
    ft.commit();
} else {
    ft.show(opisFragment);
}

Note: pr_fragment is the tag of the current fragment, the one that has the menu.

Now, this works well, but when i'm on the second fragment i want to add the functionality, that when the user clicks the back button it will show the first fragment. With this code, when i click back it exits the activity alltogether. Thank you for the help!

3 Answers 3

4

All you need is to use addToBackStack(String name) of FragmentTransaction

// Showing menu fragment also added in backstack
FragmentTransaction ft = getFragmentManager().beginTransaction();

ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
  .add(R.id.p_container, menuFragment, "menu_fragment")
  .addToBackStack("menu_fragment")
  .commit();


// Showing opis fragment also added in backstack
FragmentTransaction ft2 = getFragmentManager().beginTransaction();

ft2.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out)
      .add(R.id.p_container, opisFragment, "opis_fragment")
      .addToBackStack("opis_fragment")
      .commit();

Assuming "opis fragment" is in foreground, when you press back button, "menu_fragment" will be displayed back to the foreground, pressing back button again will exit the activity.

3
  • Oh wow this was rediciously simple. Just one more thing, how can i add an animation when i'm going from opis_fragment to menu_fragment (with the back button)?
    – Squeazer
    Dec 16, 2013 at 0:42
  • 1
    setCustomAnimations(int enter, int exit, int popEnter, int popExit) of FragmentTransaction will do the job.
    – Glenn
    Dec 16, 2013 at 0:44
  • See also this question
    – Glenn
    Dec 16, 2013 at 0:46
2

With this code, when i click back it exits the activity alltogether.

Normal because there is just your activity in your app stack. the addToBackStack() method is what you are looking for.

if (opisFragment == null) {
    opisFragment = new OpisFragment();
    ft.add(R.id.p_container, opisFragment, "opis_fragment");
    ft.addToBackStack("tag"); // <<< this line
    ft.commit();
}

From the doc :

Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

1

in mainactivity you can check fragments count if fragments count more than one we will show back button

if(getSupportFragmentManager().getBackStackEntryCount() > 0)
            {
               mDrawerToggle.setDrawerIndicatorEnabled(false);
                getSupportActionBar().setDisplayShowHomeEnabled(false);
                getSupportActionBar().setHomeButtonEnabled(true);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
            else
            {
                mDrawerToggle.setDrawerIndicatorEnabled(true);
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                getSupportActionBar().setHomeButtonEnabled(false);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.