Android - Simulate Back Button
Asked Answered
N

6

78

When i press a button in my app, I need to return to the last activity.

Any ideas?

Nessus answered 27/4, 2010 at 0:29 Comment(0)
R
99

Calling finish() from the activity you want to end should take care of this.

Edit many years later: This still works, but it's a bit of a heavy-handed approach. When I originally posted this, Fragments didn't exist, and (as several commenters have pointed out) this doesn't work quite the same way when there are Fragments involved. There are better approaches now if you're using Fragments.

Rufina answered 27/4, 2010 at 0:33 Comment(6)
No need to apologize, just happy to help!Rufina
This does not simulate the back button. If you are only using activities it is ok but if you are using fragments in an activity, this will close all the fragments with the activity together.Coy
@Coy Ah, interesting. I actually posted this answer before Fragments existed so that's interesting to know!Rufina
@Coy It seems getFragmentManager().popBackStack() does the job for fragments.Inverness
It also doesn't work when you've overridden the back button to do something else (like for highly customized layouts)Purloin
It also doesn't alllow for overriding transistions.Shaner
U
86

Just for record: The described method doesn't do the same as the back button does in some cases, but you can call

this.onBackPressed();

or

getActivity().onBackPressed();

if you are in a fragment to achieve exaclty the same behaviour.

Urchin answered 15/4, 2014 at 12:33 Comment(2)
From what I can tell, this is the best answer. Thanks.Pricecutting
This is the correct solution, but make sure you are on the UI thread or it won't work.Keifer
I
43

when using fragments:

getFragmentManager().popBackStack();

or

getSupportFragmentManager().popBackStack();

if you are using android.support.v4.app package

Ixia answered 9/5, 2013 at 19:7 Comment(1)
It didn't affect (reset) the toolbar in my case ( I was using navigation menu and many fragments declared in mobile_navigation.xml ). But it does return back.Carlettacarley
L
2

This is for a situation where the same fragment may sometimes be the only fragment in an activity, and sometimes part of a multi-fragment activity, for example on a tablet where two fragments are visible at the same time.

/**
 * Method that can be used by a fragment that has been started by MainFragment to terminate
 * itself. There is some controversy as to whether a fragment should remove itself from the back
 * stack, or if that is a violation of the Android design specs for fragments. See here:
 * https://mcmap.net/q/109727/-how-to-get-a-fragment-to-remove-itself-i-e-its-equivalent-of-finish
 */
public static void fragmentImplementCancel(Fragment fragment) {

    FragmentActivity fragmentActivity = fragment.getActivity();
    FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();

    if (fragmentManager.getBackStackEntryCount() == 1) {
        fragmentManager.popBackStack();
    }
    else {
        fragmentActivity.finish();
    }
}

This code can be called to implement a Cancel button, for example.

    if (theButton.getId() == R.id.btnStatusCancel) {
        StaticMethods.fragmentImplementCancel(this);
    }
Leban answered 3/2, 2014 at 11:23 Comment(0)
C
1

If you want to invoke back button press programmatically, new way of doing this (calling onBackPressed() is deprecated) is:

    getOnBackPressedDispatcher().onBackPressed();
Coalition answered 20/6 at 8:9 Comment(0)
P
-1

You can spoof a up button call on back button press:

@Override
public void onBackPressed() {
    onNavigateUp();
}
Prevailing answered 23/11, 2014 at 19:48 Comment(1)
The question is about to go back to last activity for the button click.Qianaqibla

© 2022 - 2024 — McMap. All rights reserved.