List transfer from one activity to another

How to pass Collections like ArrayList etc. from one Activity to another, as we used to pass Strings , int using the putExtra method for Intent?

Can someone help me as I want to pass a List<String> from one Activity to another?

+6
source share
5 answers

You can pass an ArrayList<E> in the same way if type E Serializable .

You can call putExtra (String name, Serializable value) of Intent to store and getSerializableExtra (String name) to retrieve.

Example:

 ArrayList<String> myList = new ArrayList<String>(); intent.putExtra("mylist", myList); 

In another operation:

 ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist"); 

Note that serialization can lead to performance problems: it takes time and a lot of objects will be allocated (and therefore will have to collect garbage).

+21
source

First you need to create a Parcelable object class, see an example

 public class Student implements Parcelable { int id; String name; public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int arg1) { // TODO Auto-generated method stub dest.writeInt(id); dest.writeString(name); } public Student(Parcel in) { id = in.readInt(); name = in.readString(); } public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { public Student createFromParcel(Parcel in) { return new Student(in); } public Student[] newArray(int size) { return new Student[size]; } }; } 

And the list

 ArrayList<Student> arraylist = new ArrayList<Student>(); 

Code from the calling activity

 Intent intent = new Intent(this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mylist", arraylist); intent.putExtras(bundle); this.startActivity(intent); 

Code for the called activity

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Bundle bundle = getIntent().getExtras(); ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist"); } 
+8
source

use putExtra to pass the value to the intent. use getSerializableExtra method to retrieve data like this

Action A:

 ArrayList<String> list = new ArrayList<String>(); intent.putExtra("arraylist", list); startActivity(intent); 

Activity B:

 ArrayList<String> list = getIntent().getSerializableExtra("arraylist"); 
+4
source

I tried all the suggested methods, but none of them worked and worked on my application, and then I finally was interrupted. Here is how I did it ... Basically I did this:

  List<String> myList...; Intent intent = new Intent...; Bundle b=new Bundle(); b.putStringArrayList("KEY",(ArrayList<String>)myList); intent_deviceList.putExtras(b); ....startActivity(intent); 

To get data in a new operation:

  List<String> myList... Bundle b = getIntent().getExtras(); if (b != null) { myList = bundle.getStringArrayList("KEY"); } 

Hope this helps someone ...

+3
source

To transfer an ArrayList from one activity to another, you need to enable

 intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass 

before the action begins. And to get ArrayList in another activity, enable

 Bundle bundle = getIntent().getExtras(); if (bundle != null) { temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList } 

you can pass an ArrayList through this. Hope this will be helpful for you.

0
source

Source: https://habr.com/ru/post/919734/


All Articles