Android - 模拟后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2717954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:03:58  来源:igfitidea点击:

Android - Simulate Back Button

androidbuttonback

提问by david

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

当我按下应用程序中的按钮时,我需要返回到上一个活动。

Any ideas?

有任何想法吗?

回答by Chris Thompson

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

finish()从您想要结束的活动中调用应该解决这个问题。

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

多年后编辑:这仍然有效,但这是一种粗暴的方法。当我最初发布这个时,它Fragments不存在,并且(正如一些评论者指出的那样)当Fragments涉及到时,它的工作方式并不完全相同。如果您正在使用,现在有更好的方法Fragments.

回答by u2546233

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.

如果您在片段中实现完全相同的行为。

回答by zajac.m2

when using fragments:

使用片段时:

getFragmentManager().popBackStack();

or

或者

getSupportFragmentManager().popBackStack();

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

如果您使用的是 android.support.v4.app 包

回答by RenniePet

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:
 * http://stackoverflow.com/questions/5901298/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);
    }

回答by shanik

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

您可以在按下后退按钮时欺骗向上按钮调用:

@Override
public void onBackPressed() {
    onNavigateUp();
}