后台使用后退按钮的本机 android 应用程序 [英] background a react-native android app using back button

查看:23
本文介绍了后台使用后退按钮的本机 android 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照示例模式处理 react-native docs 并且效果很好.我可以使用硬件后退按钮弹出我的导航堆栈.

I've followed the example pattern for handling the android back button in the react-native docs and it works well. I can use the hardware back button to pop my navigation stack.

虽然我没有弹出它(就像示例一样),但堆栈中只有 1 个视图,并且我从我的 hardwareBackPress 事件返回 false听众.此时,我看到在最终视图中调用了 componentWillUnmount 方法,此时我的应用程序关闭.

At the point that there's only 1 view in the stack though I don't pop it (just like the example), and I return false from my hardwareBackPress event listener. At this point it I see the componentWillUnmount method being called in my final view, at which point my app shuts down.

如果我返回 true 那么显然什么都不会发生.

If I return true then nothing happens at all obviously.

我想要发生的是该应用程序只是背景化"而不是完全退出.

What I want to happen is that the app merely gets "backgrounded" instead of exiting completely.

推荐答案

回答了我自己的问题.诀窍是覆盖 MainActiviy 中的默认后退按钮行为:

Answered my own question. The trick is to override the default back button behaviour in the MainActiviy:

public class MainActivity extends ReactActivity {

    @Override
    protected String getMainComponentName() {
        return "foo";
    }

    @Override
    public void invokeDefaultOnBackPressed() {
        // do not call super. invokeDefaultOnBackPressed() as it will close the app.  Instead lets just put it in the background.
        moveTaskToBack(true);
    }
}

这篇关于后台使用后退按钮的本机 android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆