问题
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?
回答1:
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.
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.
A sample of storing / loading a list from SharedPreferences
:
// 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;
}
回答2:
Adding my comment as an answer
If you want to keep a global variable across activities then keep that in Application. Extend a class from Application and add that in your AnddroidManifest file.
From the Android API FAQ shared by @rekaszeru
Singleton class
You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value.
回答3:
You might initialize through an Application class. This class is present across all activities. You must declare it in the Manifest file and has a life-cycle similar to an Activity.
Documentation here: Application
Hope it helps.
JQcorreia
来源:https://stackoverflow.com/questions/10623160/problems-with-singleton-in-android