Should I pass info using intent or use a static variables

你说的曾经没有我的故事 提交于 2020-02-02 09:22:28

问题


Another design question for you If I have 5 activities that can result from one activity A->B A->C A->E .... Etc

And activity A has values that need to be passed to all other activites, then do you recommned passing them through intent or should I just have a global static variables in activity A and read the values in any other activity?

Thank you


回答1:


Definitely don't use static public variables.

You should use:

  • SharedPreferences or DB for data that should be persisted (cached)
  • Intent extras if data is needed in some part of app (couple of activities)
  • Application inheritor for application-wide data, that shouldn't be persisted.



回答2:


You can subclass android.app.application and use that class to share data between activities.

public class MyApp extends Application {
  String mySharedString = "Hello World";
}

See How to declare global variables in Android?



来源:https://stackoverflow.com/questions/9547058/should-i-pass-info-using-intent-or-use-a-static-variables

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