如何在react-native中在后台杀死的Android应用程序保持状态 [英] How do I persist state for android apps killed in the background in react-native

查看:96
本文介绍了如何在react-native中在后台杀死的Android应用程序保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Android应用发送到后台时,它可以保留其实例状态,以防由于内存不足而被杀死(请参阅

When an Android App is sent to the background, it can persist its instance state in case it gets killed due to low memory (see Activity:onSaveInstanceState() and the bundle argument for Activity:onCreate(Bundle savedInstanceState)).

默认的Activity行为是保存视图层次结构的状态,因此在很多情况下,您不必编写任何代码,它可以正常工作" [tm].

The default Activity behaviour is saving the state of the view hierarchy so for a lot of cases you don't have to write any code and it "just works"[tm].

现在对于本机反应不是这样.React本机应用程序托管在单个MainActivity中,其状态包含在应用程序的javascript解释器中.

Now for react-native this is not true. React native apps are hosted in a single MainActivity and their state is contained in the javascript interpreter in the app.

我创建了一个带有简短文档的示例存储库,该文档如何在此处重现我的问题: https://github.com/einvalentin/react-native-state-test

I created a sample repository with short documentation how to reproduce my problem here: https://github.com/einvalentin/react-native-state-test

我希望react-native能够加入到本机android应用程序状态序列化机制中,同时为应用程序开发人员提供钩子以扩展他们需要自定义序列化的范围.或者,我可以看到自己将MainActivity扩展为将生命周期事件转发到Javascript层,并在那里手动进行一些自定义状态序列化-但这有点笨拙.

I would want react-native to hook into the native android app state serialisation mechanism while providing application developers hooks to extend if they need custom serialisation. Alternatively I could see myself extending the MainActivity to forward the lifecycle events to the Javascript layer and do some custom state serialisation manually there - but this feels a bit clunky.

我是否有一种明显的方式忽略了将状态保存为react-native的方式,以便在后台杀死的应用程序不会从头开始重启?例如,在与您的应用进行交互时收到电话的低内存设备上,总是会发生这种情况.

Is there an obvious way I have overlooked to save the state in react-native so that apps that are killed in the background are not restarted from scratch? This can always happen for example on low memory devices receiving a phone call while interacting with your app.

非常感谢!

推荐答案

如果您正在使用Redux进行状态管理,则可能应该使用 redux-persist .如果不是这样,您有两种选择:

If you are using Redux for state management you should probably use redux-persist. If not you have a couple of different options:

这篇关于如何在react-native中在后台杀死的Android应用程序保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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