保存多个实例,指出同一活动中的Andr​​oid [英] Save multiple instances states of the same Activity in Android

查看:128
本文介绍了保存多个实例,指出同一活动中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我想避免重装类似的数据时,它使用相同的额外来自同一个活动。

I am developing an Android app and I would like to avoid reloading similar data when it comes from the same Activity using the same extra.

具体而言,当我启动我的活动A额外,我用这额外的从服务器加载远程数据。 从这个活动,我可以重新启动'A'有不同的附加等。

Specifically, when I launch my Activity 'A' with extra, I use this extra to load remote data from server. From this Activity, I can relaunch 'A' with different extra and so on.

例如:

答:ID1 - >答:ID2 - >答:ID3

A:id1 --> A:id2 --> A:id3

但是,它也可以是一个额外的,我已经加载:

But, it can also be an extra that I already loaded :

答:ID1 - >答:ID2 - >答:ID3 - > 答:ID1

A:id1 --> A:id2 --> A:id3 --> A:id1

在这种情况下,我不会再要求服务器或失去活动堆栈。

In this case, I wouldn't request the server again or lose the activities stack.

据我了解,的onSaveInstanceState允许保存一个活动的一个实​​例,但对我来说,这是同一个活动的多个实例(带型动物演员)。

As I understand, "onSaveInstanceState" allows to save one instance of one Activity, but in my case, it's multiple instances of same Activity (with differents extras).

那么,是不是该解决方案来管理存储实例的状态(捆)的列表相同的活动?还是其他什么东西?

So, is it the solution to manage a list of saved instance states (bundle) for the same Activity ? Or something else ?

在此先感谢

推荐答案

的onSaveInstanceState 方法无法在您所描述的方式使用。 检查了这一点,它的活动类的文档,特别是活动生命周期部分。 的onSaveInstanceState 是一个被调用时,操作系统已经干掉的活动因某种原因的方法。它可以让你来填充捆绑,这将有助于重新当用户离开该活动的具体实例。通常这是因为用户切换到不同的应用程序和操作系统查杀活动回收内存,而且还发生在屏幕旋转,所以它是非常重要的,至少知道平台的细微差别。

The onSaveInstanceState method isn't used in the way you describe. Check this out, it's the documentation for the Activity Class, specifically the Activity Lifecycle section. onSaveInstanceState is a method that gets called when the OS has to kill an Activity for some reason. It allows you to populate a Bundle which will help recreate that specific instance of the Activity where the user left off. Usually this happens because the user switched to a different app and the OS is killing the Activity to reclaim memory, but also happens on screen rotation, so it's a nuance of the platform that is important to at least be aware of.

至于你的问题,我会做的是使用一个数据库来存储从服务器检索到的信息。当你开始一个活动,你可以先检查,看看是否存在于数据库中,需要填充该活动的数据。如果确实如此,负载并从那里显示它,否则使服务器呼叫

As for your question, what I would do is use a database to store the information that is retrieved from the server. When you start an Activity, you can first check to see if the data that needs to populate that Activity exists in the database. If it does, load and display it from there, else make the server call.

这是很好的,因为数据会持续在应用的多种用途。进一步展望,如果从服务器上的数据已过时的可能性,你可以轻松地扩展这个显示从数据库中最初的数据,火了一个异步请求的数据,将同时更新了用户界面和数据库时返回。您的用户几乎不可能出现的状态下,他们在等待的东西来加载,这始终是一件好事!

This is nice, because the data will be persistent over multiple uses of the App. Going further, if the data from the server has the potential to be stale, you can easily extend this to display the data from the database initially, and fire off an asynchronous request for the data that will update both the UI and database when it returns. Your user will almost never be in a state where they're waiting for things to load, which is always a good thing!

下面是关于实施SQLite数据库的基础知识一个很好的教程。< /一>这也会给你保持存储在独立的应用程序的运行数据的好处。

Here's a good tutorial on the basics of implementing an sqlite database. This will also give you the added benefit of keeping the data stored over separate runs of your application.

作为一种替代方法,如果你真的不需要数据库的持久性或其他特征和不认为的开销是值得的,你可以创建一个保存数据作为其返回轨道一个Singleton类,也许实现它使用应用类。以确保您不会丢失任何数据重要的是要注意(和我们带来了完整的圆),其存储这些数据的任何内存的方法应该用的onSaveInstanceState 进行管理,如果活动被杀害在一个意想不到的时间。

As an alternative, if you don't really need the persistence or other features of the database and don't think that the overhead is worth it, you could create a Singleton class which keeps track of the data as its returned, perhaps implementing it using the Application class. It's important to note (and bringing us full circle) that any in-memory method of storing this data should be managed with onSaveInstanceState to ensure you don't lose any data if the Activity is killed at an unexpected time.

这篇关于保存多个实例,指出同一活动中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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