Android - 模拟后退按钮

发布于 2024-08-31 02:18:05 字数 49 浏览 4 评论 0原文

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

有什么想法吗?

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

唱一曲作罢 2024-09-07 02:18:05

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

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

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.

勿忘初心 2024-09-07 02:18:05

只是为了记录:在某些情况下,所描述的方法与后退按钮的作用不同,但如果您位于片段中,您可以调用

this.onBackPressed();

or

getActivity().onBackPressed();

来实现完全相同的行为。

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.

掩于岁月 2024-09-07 02:18:05

使用片段时:

getFragmentManager().popBackStack();

或者

getSupportFragmentManager().popBackStack();

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

when using fragments:

getFragmentManager().popBackStack();

or

getSupportFragmentManager().popBackStack();

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

心奴独伤 2024-09-07 02:18:05

这是针对这样的情况:同一片段有时可能是活动中的唯一片段,有时可能是多片段活动的一部分,例如在平板电脑上,两个片段同时可见。

/**
 * 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();
    }
}

例如,可以调用此代码来实现“取消”按钮。

    if (theButton.getId() == R.id.btnStatusCancel) {
        StaticMethods.fragmentImplementCancel(this);
    }

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);
    }
飞烟轻若梦 2024-09-07 02:18:05

如果您想以编程方式调用后退按钮按下,执行此操作的新方法(不推荐调用 onBackPressed() )是:

    getOnBackPressedDispatcher().onBackPressed();

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

    getOnBackPressedDispatcher().onBackPressed();
ゃ人海孤独症 2024-09-07 02:18:05

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

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

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

@Override
public void onBackPressed() {
    onNavigateUp();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文