问题
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