使用后退按钮为本地本机android应用程序设置背景 [英] background a react-native android app using back button
问题描述
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屋!