Reputation:
I'm newcomer about the Android Studio so need to help from everyone.
I have an idea that, I want to make 1 ArrayList<>, and I will use this Array for the other Activity, but I don't know how to make.
For example MainActivity, I call this array. SecondActivity, I also call this array. In this case, I no need to make the same ArrayList for each Activity.
Is it possible to do like that? Please help
Example(); //I want to insert this to the other Activity instead of private void...
test.setOnClick...
private void Example(){
arrayList = new ArrayList<>();
arrayList.add(...)
Upvotes: 0
Views: 825
Reputation: 1637
you can use from this:
In first activity (for example MyActivity) :
public static ArrayList<String> myList=new ArrayList();
in other activity
MyActivity.myList.add("dddd");
MyActivity.myList.get(0);
Upvotes: 0
Reputation:
You could use Singleton pattern to share variable between your Activity.
public class AppData {
private List list;
private static AppData instance;
private AppData(){};
public List getList() {return this.list;}
public void setList(List list) {this.list = list;}
public static AppData getInstance() {
if (instance == null) {
instance = new AppData();
}
return instance;
}
}
//Call this in your activity
List list = AppData.getInstace().getList();
Upvotes: 1
Reputation: 3561
May be this question is duplicate but as you are new contributor and as per Stack Overflow policy i am telling you!
There are Different ways you can use them accordingly..
1- Make list static and use it anywhere with class reference like MainActivity.list;
2- Pass in your Intent as intent.putExtra("en", list);
but be sure that you implemented Serializable in your model as
public class EN implements Serializable {
//Your Model Getter setters
}
Upvotes: 3