Passing objects through intents in Android

46 views Asked by At

I wanted to clarify the process of passing a custom object (Which implements serializable) to a new activity in Android. After I create an intent I use the method putExtra with a string key and the object I want to pass. I then use getintent, and then get serializable extras. For some reason my object is brand new and not carrying over any of the data sent with it. My code looks like this.

This is the code I use to send the object

Intent intent = new Intent(Question5a1.this, ResultPage.class);
intent.putExtra("Avengers", avengers);
startActivity(intent); 

and this is the code I use to receive the object in a different activity.

Intent prevPage = getIntent();
Avengers avengers = (Avengers) prevPage.getSerializableExtra("Avengers");

I was intializing the "Avengers" object outside of the onCreate block previously, and just started intiliazing it in the lines above. Every time I tried to retrieve the String set by the object it was null. I just wanted to make sure I was correctly sending and retrieving the object.

0

There are 0 answers