0

i'm trying to pass exist object between two activities but it does not work , here is my code(it crushing in start(activity with the intent that i give him), and my class implement seriazable.

        Intent intentSecond = new Intent(this,ThirdActivity.class);
        intentSecond.putExtra("game",game);
        startActivity(intentSecond);
    }
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Annabelle
  • 11
  • 3
  • val intent = Intent(this@ClickedPlacesActivity, MapsActivity::class.java) startActivity(intent) – MMG Apr 20 '20 at 10:35
  • please use better tags in future, the 4 tags you listed has nothing to do with your question – a_local_nobody Apr 20 '20 at 10:39
  • 1
    Does this answer your question? [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – a_local_nobody Apr 20 '20 at 10:40

2 Answers2

0

Example:

public class City implements Serializable {

private List<House> house;

public List<House> getHouse() {
    return house;
}

public void setHouse(List<House> house) {
    this.house = house;
}}

Then House needs to implements Serializable as so :

public class House implements Serializable {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}}

Then you can pass city like below:

Bundle bundle = new Bundle();
bundle.putSerializable("city", city);
intent.putExtras(bundle);

And retreive it with:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
City city =  (City)bundle.getSerializable("city");
Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34
0

You can use this

   ModelObject myObj= new ModelObject();
    Gson gson = new Gson();
    String json = gson.toJson(myObj);

    Intent intentSecond = new Intent(this,ThirdActivity.class);
        intentSecond.putExtra("game",json);
        startActivity(intentSecond);

You can convert model to gson and on another page you can convert gson to model

ModelObject myObj= new Gson().fromJson(getIntent().getStringExtra("game"), ModelObject.class);
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36