Android 中存储状态

发布于 2024-12-04 19:44:37 字数 231 浏览 0 评论 0原文

我有一个列表视图,其中填充了从互联网下载的数据。 因此,在 onCreate() 方法中,我将运行异步任务来下载信息并将其放入列表视图中。

我在 saveInstanceState() 方法期间放置了一个布尔值来确定是否下载列表,如果用户通过主页按钮退出应用程序并返回,这将起作用。但是,当用户通过后退按钮退出程序时,saveInstanceState()方法不会运行。

我不想再次下载列表,如何查看之前是否下载过?

I have this listview that gets populated with the data that is downloaded from the internet.
Hence in the onCreate() method, I will run the async task to download the information and put it into the listview.

I placed a boolean value to whether if the list is downloaded or not during the saveInstanceState() method, this will work if the user gets out of the app from the home button and returns. However, when the user exits the program through the back button, the saveInstanceState() method is not run.

I do not want to download the list again, how can I check whether if it's downloaded before?

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

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

发布评论

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

评论(3

冬天的雪花 2024-12-11 19:44:37

看看这个:实现生命周期回调
http://developer.android.com/guide/topics/fundamentals/activities.html

使用回调来了解您的活动发生了什么,并在需要时做出反应。

您可以使用共享首选项来存储键值数据:
http://developer.android.com/guide/topics/data/data -storage.html

Check this out: Implementing the lifecycle callbacks
http://developer.android.com/guide/topics/fundamentals/activities.html

Use the callbacks to get noticed what happens to your activity and react if nesseceray.

You can use the Shared Preferences to store key-value data:
http://developer.android.com/guide/topics/data/data-storage.html

鹿童谣 2024-12-11 19:44:37

如果您想在应用程序的生命周期之外保留数据,则需要将其存储在 Bundle 之外。

您的选项是:

我建议使用一个私人内部存储文件来保存你的数据。

If you want to persist data beyond the LifeCycle of the application, you'll need to store it outside the Bundle.

Your options are:

I'd suggest a private Internal Storage file for saving your data.

撩心不撩汉 2024-12-11 19:44:37

您可以重写 Activity.onBackPressed() 并在那里保存您的状态。

另外,你可以做

void onPause() {
    if(isFinishing()) {
       // save your state. maybe you'd be better off with using preferences (or other things suggested by people here), so you don't need to worry about the instance state.
    }
}

You can override Activity.onBackPressed() and save your state there as well.

Also, you can do

void onPause() {
    if(isFinishing()) {
       // save your state. maybe you'd be better off with using preferences (or other things suggested by people here), so you don't need to worry about the instance state.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文