Passing objects from one activity to another activity

While developing on android platform we often face requirements where we have to pass objects from one activity to another. The officially recommended way of passing objects is to make your object implement Parcelable interface.

In this article we will learn that you dont have to implement Parcelable interface to pass objects from one activity to another activity and this approach has certain pitfalls. The non-primitive variables will not get parceled unless they implement parcelable Interface. Timestamp’s do not get parceled. There are fixes for parceling timestamp and non-primitive objects but they get messy, complicated and also buggy.

Implementing Parcelable also makes your code looks ugly and it will bother you if you want to see elegant and simple to understand code.

An alternative to implementing Parcelable interface is to encode objects into JSON String using GSON Converter and then pass the string with the intent to the another activity. On the receiver end you decode the JSON string into an actual Object.

If you are a beginner then you may not understand what i am saying here. You should refer and learn the concept of JSON parsing and GSON converter. After you have learned these concepts you will be able to understand this topic.

Let us try to see that in action how we use GSON Converter to pass objects from one activity to another.

Case One : Passing a Single Object

In this example i am passing a custom object named “ItemImage” you can replace ItemImage with any other object of your choice.

Step 1 : Encode the Object into a JSON String

ItemImage itemImage = new ItemImage

Gson gson = new Gson();
String jsonString = gson.toJson(itemImage);

Intent intent = new Intent(this,ReceiverActivity.class)
intent.putExtra(jsonString,ITEM_IMAGE_INTENT_KEY)
startActivity(intent)

In this example we see how we have encoded our custom object into a String using GSON converter. You can passing this string from one activity to another using regular intents.

 

Step 2 : Decode the Object from a JSON String

String itemJson = getActivity().getIntent().getStringExtra(ITEM_IMAGES_INTENT_KEY);

Gson gson = new Gson();
ItemImage itemImage = gson.fromJson(itemJson, ItemImage.class);

On the receiving end we will receive our encoded JSON String. We can decode this JSON String back to our object using the GSON converter.

Case Two : Passing a List of Objects

In the above example we saw how we can pass a single object. But the procedure for passing a list of objects is somewhat different. Therefore we will discuss here how we can pass a list of objects from one activity to another.

In this example i am passing a list of ItemImage objects . You can replace ItemImage with any other object of your choice.

Step 1: Encode the List into a JSON String

List list = new ArrayList<>();

Gson gson = new Gson();
String jsonString = gson.toJson(list);

Intent intent = new Intent(this,ReceiverActivity.class)
intent.putExtra(jsonString,ITEM_IMAGE_INTENT_KEY)
startActivity(intent)

We have encoded the list into a JSON String. We can now pass this string from one activity to another activity using regular intents.

Note : If you are a beginner and dont know how to pass a string from one activity to another activity please go through an introductory tutorial on how to pass a string using an intent. You can find that tutorial using a google search.

Step 2 : Decode the List from the JSON String

String itemJson = getActivity().getIntent().getStringExtra(ITEM_IMAGES_INTENT_KEY);


Type listType = new TypeToken(){}.getType();
Gson gson = new Gson();
dataset = gson.fromJson(itemJson, listType);

We have received JSON string in the another object. Now we have to decode the JSON string

 

Advantages of using GSON converter over Implementing Parcelable Interface

  1. You dont have to implement another interface for your existing objects. GSON converter will just convert any object into a string and a string back to Objects.
  2. Timestamps get transferred easily without any issues.
  3. The non-primitive instance variables gets transferred easily without implementing any new interface

 

PS : Please note that instead of using a GSON converter you may use any other converter of your choice.

Leave a Reply

Your email address will not be published. Required fields are marked *