I have a question for Android Serializable.

Asked 2 years ago, Updated 2 years ago, 104 views

I'd like to ask you a question about Serializable Intent.

If you serialize the data class of ArrayList and simply send putExtra, the receiving side will receive it Do I just have to do getSerializableExtra? Also, I would appreciate it if you could tell me how to check if the data arrived well.

public class ItemData implements Serializable {

String title;
String maintext;

public SingerItem(String title, String maintext) {
    this.title = title;
    this.maintext = maintext;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getMaintext() {
    return maintext;
}

public void setMaintext(String maintext) {
    this.maintext = maintext;
}
}

Sender

 Intent intent = new Intent(getApplicationContext(), secondActivity.class);
            ArrayList<ItemData > items = new ArrayList<ItemData >();
            intent.putExtra("items", items);
            startActivity(intent);

Receiving party

  Intent intent = getIntent();
    ArrayList<ItemData > dataset = (ArrayList<ItemData >) intent.getSerializableExtra("items");

serializable android arraylist intent

2022-09-21 21:28

1 Answers

Yes, I think you can do it the way above. If it is not delivered normally from the int, we will return the null, so please add the null check part.

Intent = getIntent();
ArrayList<ItemData > dataset = (ArrayList<ItemData >) intent.getSerializableExtra("items");

if(dataset != null){
    //do something
}


2022-09-21 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.