简体   繁体   English

如何在Android中的onStop / onPause活动中保存对象列表

[英]How save list of objects onStop/onPause of activity in android

Lately writing some app I've been stopped by one problem. 最近写了一些应用程序,我被一个问题拦住了。

In this app I have two activities: A and B. In activity AI am downloading list of objects from web each time when GPS coordinates change. 在此应用程序中,我有两个活动:A和B。在活动AI中,每次GPS坐标更改时,AI都会从Web下载对象列表。 To this activity there is connected bound service with GPS and Internet connection call. 为此活动提供了具有GPS和Internet连接呼叫的连接绑定服务。 Those objects are then put into list created from their names and after pressing on any of this I can open the B activity passing this object and in activity B get some more info from this object. 然后将这些对象放入根据它们的名称创建的列表中,并在按任意一个对象之后,我可以打开通过该对象的B活动,并在活动B中从该对象获取更多信息。

Activity B hasn't got any bound service, it's only taking what is inside object and display it. 活动B没有任何绑定服务,它仅获取对象内部的内容并显示它。

Because bound service in activity A is unbound in onStop then when I come back to it using a back arrow it takes a while to download everything again. 因为活动A中的绑定服务在onStop中是未绑定的,所以当我使用后退箭头返回它时,需要一段时间才能再次下载所有内容。 What is more interesting, the list is still filled, but those are only names, the actual list of objects doesn't exist and pressing just after comeback on any of names will cause OutOfBoundException. 更有趣的是,列表仍然被填充,但是这些仅仅是名称,对象的实际列表不存在,并且在任何名称恢复使用后立即按下将导致OutOfBoundException。

So, my question is, what would you advice to do to save this objects when I am living activity A and retrieve them when come back, before download of the new (or this same) list. 因此,我的问题是,在下载新的(或相同的)列表之前,当我进行活动A时,您将建议如何保存该对象并在回来时取回它们?

I've tried: 我试过了:

  • onRestoreInstanceState() with onSaveInstanceState() , because there you can save parcelable list, but the activity A isn't destroyed so fast, so there is no call for onRestoreInstanceState() ; onRestoreInstanceState()onSaveInstanceState() ,因为您可以在其中保存可打包清单,但是活动A并没有那么快被销毁,因此没有对onRestoreInstanceState()调用;
  • Shared preferences , but it was bad choice, because you can't save list there; Shared preferences ,但这不是一个好选择,因为您无法在此处保存列表;
  • ContentValues and content resolvers but also, bad choice, because you can't save list. ContentValuescontent resolvers也很糟糕,因为您无法保存列表。

Do you have any more ideas, how it can be done? 您还有其他想法,如何实现?

Oh, I've forget to mention, I've taken assumption that there can be mximum 100 objects in list, and each object have 5 fields. 哦,我忘了提, 我假设列表中最多可以包含100个对象,每个对象有5个字段。

create a Singleton class, and store the objects in a HashMap or ArrayList (whatever is suitable for you) and access that list in other activity. 创建一个Singleton类,然后将对象存储在HashMap或ArrayList中(适合您的对象),并在其他活动中访问该列表。

import java.util.ArrayList;

public class DataHandler {
    private static DataHandler handler;
    private ArrayList<Object> list;

    private DataHandler() {
    }

    public static DataHandler getInstance() {
        if (handler == null)
            handler = new DataHandler();
        return handler;
    }

    public ArrayList<Object> getList() {
        return list;
    }

    public void setList(ArrayList<Object> list) {
        this.list = list;
    }
}

If the reason for not using SharedPreferences and ContentValues is because it can't handle lists then you might consider serializing your list to something like JSON, save it as a String and when loading map it back to objects. 如果不使用SharedPreferences和ContentValues的原因是因为它无法处理列表,那么您可以考虑将列表序列化为JSON之类的内容,将其另存为String并在加载时映射回对象。 It's usually 2 lines of code to transform object <-> JSON for example. 例如,转换对象<-> JSON通常需要两行代码。

You need to cache the downloaded data in a singleton class like here 您需要将下载的数据缓存在单例类中,如下所示

class DataController { 类DataController {

public static ArrayList aa; 公共静态ArrayList aa;

} }

fill this arraylist when you download data and in your activity check if this list is not null then use this arraylist only instead of downloading it. 在下载数据时填写此arraylist,并在活动中检查此列表是否不为null,然后仅使用此arraylist而不是下载它。 do some more research on singleton class concept and I am sure you will learn something very helpful to you. 对单例课程概念进行更多研究,我相信您会学到一些对您很有帮助的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM