问题
I'm creating an app where I need to pass relatively complex classes between several Activity
s and Service
s. Lets say something like this:
public class A implements Serializable{
int myInt;
String myString;
B myB;
}
public class B implements Serializable{
ArrayList<String> myStrings;
}
Now this can be done by Intent.putExtra(String, Serializable)
, but I could also create a singleton class holding the A
instance. Using a singleton provides easy access to my instance of A
, but it seems a bit 'hacky' to me.
What are the up and down sides of using both methods? Are there any strict reasons why I shouldn't use one of the methods?
回答1:
you could try this share data between activities.
You could use intents, Singletons class, A public static field/method, A HashMap of WeakReferences to Objects, Application Preferences, Files, contentProviders, SQLite DB.
Look here Android FAQ
来源:https://stackoverflow.com/questions/11679574/app-design-pass-date-through-intents-or-use-singletons