Android的 - 保存数据时关闭的应用程序和检索数据 [英] Android - Saving data upon closing app and retrieving that data

查看:315
本文介绍了Android的 - 保存数据时关闭的应用程序和检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,有很多问题的问候保存/取回放在这里的数据。我在做觉得看东西我自己真的以为我会设法找到我的答案,而不必问一个问题,但我开始怀疑的东西,我还没有看到在这里的答案。

I know, there are plenty of questions in regards to saving/retrieving data on here. I was doing find looking things up on my own and really thought I could manage to find my answers without having to "ask a question", but I began to wonder something that I haven't seen an answer for on here.

我的情况:

当然,我在做一个应用程序。在关闭应用程序,我想保存的数字(0或1)或布尔值,因为它是一个简单的数组。在启动应用程序,我想搜索的数组,如果它存在,并检索它的应​​用程序中使用。

Naturally, I'm making an app. Upon closing the app, I want to save a simple array of numbers (0 or 1) or boolean values as it were. Upon starting the app, I want to search for that array, if it exists, and retrieve it for use within the app.

我开始把我的code到其中的阵列将用于该活动。但是,我开始想,如果我必须复制/粘贴覆盖的onStop()函数将我所有的活动?还是我做的主要活动,并以某种方式连接的其他活动。

I began placing my code into the activity in which the array would be used. But, I started wondering if I would have to copy/paste the overridden onStop() function into all of my activities? Or do I do it in the main activity and somehow link the other activities.

基本上,无论处于何种状态/活动目前的应用程序是,当应用程序被关闭,我希望能够保存INT /布尔数组,并打开它时,应用程序启动备份。

Basically, no matter what state/activity the app is currently on when the app is closed, I want to be able to save the array of int/bool and open it back up when the app is started.

也许我不知道如何寻找我想要的东西,所以解释感觉就像在做正确的事。

Maybe I didn't know how to search for what I wanted, so explaining it felt like the right thing to do.

我不介意做更多的搜索,但如果有人在最​​起码点我在正确的方向,我会非常感激。

I don't mind doing more searching, but if someone would point me in the right direction at the very least, I'd be extremely grateful.

感谢

编辑:如果有一个更好的方式做我想做的比我描述(即使用不同的状态,而不是的onStop(),例如),请随时扔出去的想法。这是我第一次真正有处理活动的生命周期,我有点糊涂了,即使期待通过Android开发教程。我真的认为他们在大多数情况下做得不好。

If there's a better way to do what I want than what I described (ie. using a different state instead of onStop(), for instance), please feel free to throw out ideas. This is my first time actually having to deal with the activities' lifecycles and I'm a bit confused even after looking through the android development tutorials. I really think they're poorly done in most cases.

推荐答案

在应用程序需要保存一些持久性数据,你应该始终做到在的onPause()方法而不是的onStop()。因为如果Android操作系统杀死你的进程,然后的onStop()的onDestroy()方法不会被调用。同样检索 onResume数据()方法。

When you application needs to save some persistent data you should always do it in onPause() method and rather than onStop(). Because if android OS kills your process then onStop() and onDestroy() methods are never called. Similarly retrieve data in onResume() method.

这篇关于Android的 - 保存数据时关闭的应用程序和检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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