在Android的单身问题 [英] Problems with singleton in android
问题描述
我有一个Android应用程序,其中有几个活动。每个活动的下载XML / JSON提要,分析它,并推动了一个单通常作为一个ArrayList。
I have an android app, where there are couple of activities. Each of the activities download an xml/json feed, parse it and push that to a singleton usually as an arraylist.
通过各种活动去后然而,似乎单身正在死去,最早期的下载数组列表是空的现在。为什么会这样呢?是不是跨活动共享数据独居建议的做法?
However after going through various activities, It seems the singleton is dying and most of the earlier downloaded array lists are now empty. Why is it so? Isn't singleton a recommended practice for sharing data across activities?
推荐答案
请参阅的 Android的API常见问题解答,在这里你一定会找到你的问题的最合适的解决方案(你是否需要一个短版活还是长寿命的数据)。
Please see this thread of Android API FAQ, here you will surely find the most appropriate solution for your problem (whether you need a short-living or a long-life data).
单身用于短生命周期对象。
Singletons are used for short life-cycle objects.
修改:请记住,对于数据的广泛的量它始终是一个更好的主意,使用数据库/ structurated本地文件(尽管后者是消耗资源的读/写)。如果你是非常复杂的对象的工作,选择共享preferences
可能会导致一些头痛了。
Edit: Please keep in mind, that for extensive amount of data it is always a better idea to use databases / structurated local files (though the latter are resource-consuming to read/write). And if you are working with very complex object, choosing SharedPreferences
might cause some headache too.
存储/从加载列表共享preferences的样本
:
// the name with which to store the shared preference
public static final String PERSON_LIST = "my.personlist";
// a sample data structure that will be stored
public static final class Person
{
private String name;
private int age;
public String toString()
{
return name + "|" + age;
}
// splits the passed String parameter, and retrieves the members
public static Person loadPersonFromString(final String personString)
{
final Person p = new Person();
final String[] data = personString.split("|");
p.name = data[0];
p.age = Integer.parseInt(data[1]);
return p;
}
}
/**
* Saves the current list of Persons in SharedPreferences
* @param persons: the list to save
*/
public void savePersonList(final ArrayList<Person> persons)
{
final SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getApplicationContext());
final Editor editor = prefs.edit();
final StringBuilder builder = new StringBuilder();
for (final Person person : persons)
{
if (builder.length() > 0)
builder.append("-Person-");
builder.append(person.toString());
}
editor.putString(PERSON_LIST, builder.toString());
editor.commit();
}
/**
* Loads the list of Persons from SharedPreferences
* @return the loaded list
*/
public ArrayList<Person> loadPersonList()
{
final SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getApplicationContext());
final ArrayList<Person> persons = new ArrayList<Menu.Person>();
final String[] array = prefs.getString(PERSON_LIST, "").split("-Person-");
for (final String personString : array)
{
if (personString.length() > 0)
persons.add(Person.loadPersonFromString(personString));
}
return persons;
}
这篇关于在Android的单身问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!