简体   繁体   中英

Should I pass info using intent or use a static variables

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM