info from 1st activity to a fourth activity
What is the best way to pass information from an activity one to a fo开发者_如何学编程urth activity? In my case, the first activity is a user form. It has a button that verifies the data and opens a second activity, where the user has different option to choose. Each option is a new activity. Some of those activities need part of the information that was entering by the user in the first activity.
My approach is to pass the data using putExtras in the intent from first activity to second activity. In the second activity I get the Extras and place again in another intent with putExtras to pass it to the next activity and continue doing that until reach the activity that needs the info.
This approach seems that I am writting the same code over and over again. So can someone give some different options?
Option (a) - have all the activities extend a base activity that has the code for putting and getting the extras written only once.
Option (b) - Put the data in a singleton, and get it from there when needed
enum DataStore {
INSTANCE;
private Object data;
public void putData(Object data) {
this.data = data;
}
public Object getData() {
return data;
}
And then use putData on the first activity:DataStore.INSTANCE.putData(yourData)
and fetch it from the forth Object yourdata = DataStore.INSTANCE.getData();
精彩评论