I need to get an ArrayList I create and use in another activity, so I can get data from it while in another activity. The array is of an object, which has 3 strings in it. How to do that?
            Asked
            
        
        
            Active
            
        
            Viewed 62 times
        
    -2
            
            
         
    
    
        Michał Turczyn
        
- 32,028
- 14
- 47
- 69
 
    
    
        Kazhiunea
        
- 11
- 3
- 
                    5Possible duplicate of [How to send an object from one Android Activity to another using Intents?](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – terencey May 05 '18 at 12:15
4 Answers
0
            
            
        You can put your your Arraylist in intent that you will use to start the activity. But make sure the pojo implements Serializable or Parcelable
 
    
    
        Kruti Parekh
        
- 1,271
- 9
- 21
0
            
            
        Please try below code It may be helpful to solve your issue :
You are creating two-way Parcelable and Serialization. I have created Parcelable.
UserBean :
public class User implements Parcelable {
    String id,firstname,lastname;
    public User(String id, String firstname, String lastname) {
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
    }
    protected User(Parcel in) {
        id = in.readString();
        firstname = in.readString();
        lastname = in.readString();
    }
    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }
        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(firstname);
        dest.writeString(lastname);
    }
}
MainActivity :
public class MainActivity extends AppCompatActivity {
    private ArrayList<User> userArrayList;
    private Button btnUserData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        userArrayList = new ArrayList<User>();
        btnUserData = (Button)findViewById(R.id.btnUserData);
        btnUserData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User userOne = new User("1","Kaushal","Gosaliya");
                User userTwo = new User("2","Viral","Shah");
                User userThree = new User("3","Shalin","Gosaliya");
                userArrayList.add(userOne);
                userArrayList.add(userTwo);
                userArrayList.add(userThree);
                startActivity(new Intent(MainActivity.this,UserActivity.class).putExtra("userdata",userArrayList));
            }
        });
    }
}
MainActivity XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <Button
        android:id="@+id/btnUserData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
UserActivity :
public class UserActivity extends AppCompatActivity {
    ArrayList<User>  userArrayList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        if(getIntent().hasExtra("userdata")){
            userArrayList = getIntent().getParcelableArrayListExtra("userdata");
        }
        if(userArrayList != null){
            for(User user :userArrayList)
               Toast.makeText(this, " Id: "+ user.getId() + " First Name: " + user.getFirstname() + " Last Name:" + user.getLastname(), Toast.LENGTH_SHORT).show();
        }
    }
}
 
    
    
        Kaushal Gosaliya
        
- 421
- 7
- 16
0
            
            
        You can define your ArrayList as static so you can access it from any class or activity. like this :
public static ArrayList<String> = new ArrayList<>();
 
    
    
        Majd
        
- 82
- 5
0
            
            
        The way you handle it base on how you use your data:
- If your list is created and mainly used in Activity1,Activity2only use it for a while then finish to return back toActivity1again. Then you can useyourIntent.putParcelableArrayListExtra
- If your list is used globally many places in your project, it should be made staticinstead to reduce many parcel/unparcel processes
 
    
    
        Tam Huynh
        
- 2,026
- 1
- 16
- 20