App design: Pass date through Intents or use Singletons?

时光毁灭记忆、已成空白 提交于 2019-12-11 22:55:44

问题


I'm creating an app where I need to pass relatively complex classes between several Activitys and Services. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!