Handling back button press Inside Fragments

Affordable-Papers.net

12 thoughts on “Handling back button press Inside Fragments

  1. Could you not acquire the currently active fragment inside the activity by popping the backstack? It seems weird to me to have the fragment tell its activity what is currently supposed to be the active fragment.
    I haven’t tried this, but here’s how it could possibly work:

    public class MyActivity {
    public void newFragment(String fragmentTag) {
    MyFragment myFragment = MyFragment.newInstance(fragmentTag);
    FragmentManager.beginTransaction()
    // give the BackStackEntry the same name as the fragment
    .add(myFragment, fragmentTag)
    .addToBackStack(fragmentTag)
    .commit;
    }

    @Override
    public void onBackPressed() {
    //get the name from the topmost BackStackEntry which is also the fragment tag.
    String fragmentTag =
    FragmentManager.getBackStackEntryAt(FragmentManager.getBackStackCount()-1)
    .getName();

    MyFragment topmostFragment = FragmentManager.findFragmentByTag(fragmentTag);
    if (topmostFragment == null || !(topmostFragment instanceof BackHandledFragment) || !((BackHandledFragment) topmostFragment.onBackPressed())) {
    super.onBackPressed();
    }
    FragmentManager.popBackStack() //always pop?
    }

    }

    By having the same name for the BackStackEntry as for its associated Fragment you could avoid the BackHandlerInterface altogether which I think is preferable since you generally don’t want Fragments interfering with the Activity’s interchanging of Fragments.
    But like I said, it’s just a thought. I haven’t tried if this actually works.

  2. A clever idea. Thanks for posting.

    I think this may run into difficulties if you have an application built around multiple fragments where only one or two is using the BackHandledFragment mechanism. After the BackHandledFragment has come and gone and another (non-BackHandledFragment) fragment has become active, the activity is left with a reference to a stale fragment in “selectedFragment”. This object won’t be recycled (memory leak) because an active Activity holds a reference to it, and this fragment’s onBackPressed() method will still be called, even though it is not the active fragment with which the user was interacting when pressing Back.

    Chris’ proposal looks like a viable way to address those issues. No stale references to obsolete fragments, and only calling fragments if they are at the top of the stack.

  3. Has anyone actually made this work? From what I’ve learned in my efforts, Activity.onBackPressed() doesn’t actually get called unless the back-press would result in Activity.finish() being called. In other words, when a back-stacked fragment is on the top of the stack, Activity.onBackPressed() does _not_ get called as Android sends the event to the fragment manager instead, so this mechanism can’t forward that event to the fragment.

  4. I have two fragments and on each fragment their is one API call.But I want that when I press back button from second fragment then it’s simply removed and make first fragment visible .

    Basically I want to avoid API call on OnBackPress() method.

    How can I accomplish this?

  5. Thank you for the blog. By following your code I was implement the Back Fragment. And now my fragment can handle back presses!!! Thank you so much. Very simple and elegant solution.

  6. A very great idea and it works effectively. One thing I am stuck at is.
    Now when I press the back button in the fragment I come back to the activity(mission 1 complete)
    Now in the activity when I press back button, the same activity is called again and again. What i want to do is when the context is my current activity and when I press back button something else should happen. It would be really great if anybody could provide the solution to this. Thanks

  7. Wow!! Amazing.. worked perfectly for me! Thanks a lot for a simple and straight forward approach

Leave a Reply

Your email address will not be published. Required fields are marked *