简体   繁体   English

从第一活动到第四活动的信息

[英]info from 1st activity to a fourth activity

What is the best way to pass information from an activity one to a fourth 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. 我的方法是在意图中使用putExtras从第一活动传递数据到第二活动。 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. 在第二个活动中,我获得了Extras,并再次将其与putExtras放置在另一个意图中,以将其传递到下一个活动,并继续执行该操作,直到到达需要信息的活动为止。

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. 选项(a)-让所有活动扩展一个基本活动,该活动具有用于放置和获取额外内容的代码仅编写一次。

Option (b) - Put the data in a singleton, and get it from there when needed 选项(b)-将数据​​放在一个单例中,并在需要时从那里获取

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(); 然后在第一个活动上使用putData: DataStore.INSTANCE.putData(yourData)并从DataStore.INSTANCE.putData(yourData) Object yourdata = DataStore.INSTANCE.getData(); DataStore.INSTANCE.putData(yourData)其获取Object yourdata = DataStore.INSTANCE.getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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