Android 中存储状态
我有一个列表视图,其中填充了从互联网下载的数据。 因此,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这个:实现生命周期回调
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
如果您想在应用程序的生命周期之外保留数据,则需要将其存储在 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.
您可以重写 Activity.onBackPressed() 并在那里保存您的状态。
另外,你可以做
You can override Activity.onBackPressed() and save your state there as well.
Also, you can do