since this question and some of the answers are over five years old, let me share my solution. This is a follow-up and modernization to the answer from @oyenigun
UPDATE:
At the bottom of this article, I added an alternative implementation using an abstract Fragment extension that won't involve the Activity at all, which would be useful for anyone with a more complex fragment hierarchy involving nested fragments that require different back behavior.
I needed to implement this because some of the fragments I use have smaller views that I would like to dismiss with the back button, such as small information views that pop up, etc, but this is good for anyone who needs to override the behavior of the back button inside fragments.
First, define an Interface
public interface Backable {
boolean onBackPressed();
}
This interface, which I call Backable
(I'm a stickler for naming conventions), has a single method onBackPressed()
that must return a boolean
value. We need to enforce a boolean value because we will need to know if the back button press has "absorbed" the back event. Returning true
means that it has, and no further action is needed, otherwise, false
says that the default back action still must take place. This interface should be it's own file (preferably in a separate package named interfaces
). Remember, separating your classes into packages is good practice.
Second, find the top fragment
I created a method that returns the last Fragment
object in the back stack. I use tags... if you use ID's, make the necessary changes. I have this static method in a utility class that deals with navigation states, etc... but of course, put it where it best suits you. For edification, I've put mine in a class called NavUtils
.
public static Fragment getCurrentFragment(Activity activity) {
FragmentManager fragmentManager = activity.getFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 0) {
String lastFragmentName = fragmentManager.getBackStackEntryAt(
fragmentManager.getBackStackEntryCount() - 1).getName();
return fragmentManager.findFragmentByTag(lastFragmentName);
}
return null;
}
Make sure the back stack count is greater than 0, otherwise an ArrayOutOfBoundsException
could be thrown at runtime. If it isn't greater than 0, return null. We'll check for a null value later...
Third, Implement in a Fragment
Implement the Backable
interface in whichever fragment where you need to override the back button behavior. Add the implementation method.
public class SomeFragment extends Fragment implements
FragmentManager.OnBackStackChangedListener, Backable {
...
@Override
public boolean onBackPressed() {
// Logic here...
if (backButtonShouldNotGoBack) {
whateverMethodYouNeed();
return true;
}
return false;
}
}
In the onBackPressed()
override, put whatever logic you need. If you want the back button to not pop the back stack (the default behavior), return true, that your back event has been absorbed. Otherwise, return false.
Lastly, in your Activity...
Override the onBackPressed()
method and add this logic to it:
@Override
public void onBackPressed() {
// Get the current fragment using the method from the second step above...
Fragment currentFragment = NavUtils.getCurrentFragment(this);
// Determine whether or not this fragment implements Backable
// Do a null check just to be safe
if (currentFragment != null && currentFragment instanceof Backable) {
if (((Backable) currentFragment).onBackPressed()) {
// If the onBackPressed override in your fragment
// did absorb the back event (returned true), return
return;
} else {
// Otherwise, call the super method for the default behavior
super.onBackPressed();
}
}
// Any other logic needed...
// call super method to be sure the back button does its thing...
super.onBackPressed();
}
We get the current fragment in the back stack, then we do a null check and determine if it implements our Backable
interface. If it does, determine if the event was absorbed. If so, we're done with onBackPressed()
and can return. Otherwise, treat it as a normal back press and call the super method.
Second Option to not involve the Activity
At times, you don't want the Activity to handle this at all, and you need to handle it directly within the fragment. But who says you can't have Fragments with a back press API? Just extend your fragment to a new class.
Create an abstract class that extends Fragment and implements the View.OnKeyListner
interface...
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public abstract class BackableFragment extends Fragment implements View.OnKeyListener {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(this);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackButtonPressed();
return true;
}
}
return false;
}
public abstract void onBackButtonPressed();
}
As you can see, any fragment that extends BackableFragment
will automatically capture back clicks using the View.OnKeyListener
interface. Just call the abstract onBackButtonPressed()
method from within the implemented onKey()
method using the standard logic to discern a back button press. If you need to register key clicks other than the back button, just be sure to call the super
method when overriding onKey()
in your fragment, otherwise you'll override the behavior in the abstraction.
Simple to use, just extend and implement:
public class FragmentChannels extends BackableFragment {
...
@Override
public void onBackButtonPressed() {
if (doTheThingRequiringBackButtonOverride) {
// do the thing
} else {
getActivity().onBackPressed();
}
}
...
}
Since the onBackButtonPressed()
method in the super class is abstract, once you extend you must implement onBackButtonPressed()
. It returns void
because it just needs to perform an action within the fragment class, and does not need to relay the absorption of the press back to the Activity. Make sure you do call the Activity onBackPressed()
method if whatever you're doing with the back button doesn't require handling, otherwise, the back button will be disabled... and you don't want that!
Caveats
As you can see, this sets the key listener to the root view of the fragment, and we'll need to focus it. If there are edit texts involved (or any other focus-stealing views) in your fragment that extends this class, (or other inner fragments or views that have the same), you'll need to handle that separately. There's a good article on extending an EditText to lose focus on a back press.
I hope someone finds this useful. Happy coding.