Sharing Object Between Activities In Android

Introduction

 
In an Android application, there will be use cases where we need to pass data from one activity to another activity. Sometimes maybe one or two pieces of data; sometimes we need to pass lots of data or class objects. Here I am describing how we can share an object in between.
 
For passing primitive data types like String integer floats, we can pass them through intent by putting the unique key in the put extra function of intent. Suppose, we want to pass java objects from one activity to another.  Then there we've got a parcel and serializable.
 
Both of them are similar, but the difference is parcel is well defined in the Android SDK and it is fast as compared to serializable, which is is the component in Java. And one more thing which I experienced while using both of them is that in parcel the garbage collection is less.
 
For creating parceble class first we need to implement the Parceble interface and override the writeToParcel() method in its own class and then write all object members into parceble objects. The next things are to create a parceble Creator object to deserialize Java objects.
Here I am going to explain with some examples;  in this I want to pass Doctor details from one activity to another.
  1. public class DoctorSearchPojo implements Parcelable   
Then implement the Parceble.Creator,
  1. public static final Creator < DoctorSearchPojo > CREATOR = new Creator < DoctorSearchPojo > () {  
  2.     @Override  
  3.     public DoctorSearchPojo createFromParcel(Parcel in ) {  
  4.         return new DoctorSearchPojo( in );  
  5.     }  
  6.     @Override  
  7.     public DoctorSearchPojo[] newArray(int size) {  
  8.         return new DoctorSearchPojo[size];  
  9.     }  
  10. };  
  11. @Override  
  12. public int describeContents() {  
  13.     return 0;  
  14. }  
This is for deserializing the Java objects.
 
Now, declare the member variables.
  1. private String speciality = null;  
  2. private String gender = null;  
  3. private int maxPrice = 0;  
  4. private String city = null;  
  5. private int minPrice = 0;  
  6. private int rating = 0;  
  7. private String language = null;  
  8. private String status = null
And then create the getter and setter method for them.
  1. public String getSpeciality() {  
  2.     return speciality;  
  3. }  
  4. public void setSpeciality(String speciality) {  
  5.     this.speciality = speciality;  
  6. }  
  7. public String getGender() {  
  8.     return gender;  
  9. }  
  10. public void setGender(String gender) {  
  11.     this.gender = gender;  
  12. }  
  13. public int getMaxPrice() {  
  14.     return maxPrice;  
  15. }  
  16. public void setMaxPrice(int maxPrice) {  
  17.     this.maxPrice = maxPrice;  
  18. }  
  19. public String getCity() {  
  20.     return city;  
  21. }  
  22. public void setCity(String city) {  
  23.     this.city = city;  
  24. }  
  25. public int getMinPrice() {  
  26.     return minPrice;  
  27. }  
  28. public void setMinPrice(int minPrice) {  
  29.     this.minPrice = minPrice;  
  30. }  
  31. public int getRating() {  
  32.     return rating;  
  33. }  
  34. public void setRating(int rating) {  
  35.     this.rating = rating;  
  36. }  
  37. public String getLanguage() {  
  38.     return language;  
  39. }  
  40. public void setLanguage(String language) {  
  41.     this.language = language;  
  42. }  
  43. public String getStatus() {  
  44.     return status;  
  45. }  
  46. public void setStatus(String status) {  
  47.     this.status = status;  
Now, I am creating a hashmap to store these values as key-value pair.
  1. HashMap < String, String > searchData = new HashMap < String, String > ();  
  2. public HashMap < String, String > getMapSearchData() {  
  3.     return searchData;  
  4. }  
  5. public void setMapSearchData(HashMap < String, String > searchData) {  
  6.     this.searchData = searchData;  
And create the getter and setter methods for them also.
 
Now, we need to create a private constructor for the parceble class.
  1. protected DoctorSearchPojo(Parcel in ) {  
  2.     speciality = in .readString();  
  3.     gender = in .readString();  
  4.     language = in .readString();  
  5.     status = in .readString();  
  6.     city = in .readString();  
  7.     rating = in .readInt();  
  8.     maxPrice = in .readInt();  
  9.     minPrice = in .readInt();  
  10.     final int N = in .readInt();  
  11.     for (int i = 0; i < N; i++) {  
  12.         String key = in .readString();  
  13.         String value = in .readString();  
  14.         searchData.put(key, value);  
  15.     }  
Which will put every value in the hash map and also in the member variables.
 
Now, we need to override the writeToParcel method which is assigning values to member variables,
  1. @Override  
  2. public void writeToParcel(Parcel dest, int i) {  
  3.     dest.writeString(speciality);  
  4.     dest.writeString(gender);  
  5.     dest.writeString(language);  
  6.     dest.writeString(status);  
  7.     dest.writeString(city);  
  8.     dest.writeInt(rating);  
  9.     dest.writeInt(maxPrice);  
  10.     dest.writeInt(minPrice);  
  11.     final int N = searchData.size();  
  12.     // dest.writeInt(N);  
  13.     if (N > 0) {  
  14.         for (Map.Entry < String, String > entry: searchData.entrySet()) {  
  15.             dest.writeString(entry.getKey());  
  16.             String dat = entry.getValue();  
  17.             dest.writeString(dat);  
  18.         }  
  19.     }  
  20. }  
Now, our parceble class is ready and we can also create a constructor for setting the variables easily.
  1. public DoctorSearchPojo(String speciality, String gender, int maxPrice, String city, int minPrice, int rating, String language, String status) {  
  2.     this.speciality = speciality;  
  3.     this.gender = gender;  
  4.     this.maxPrice = maxPrice;  
  5.     this.city = city;  
  6.     this.minPrice = minPrice;  
  7.     this.rating = rating;  
  8.     this.language = language;  
  9.     this.status = status;  
Now, from the first activity/fragment set the model class and values by caling the constructor.
  1. doctorSearchPojo = new DoctorSearchPojo(model.getName(), "", 0, city, 0, 0, """");  
  2. Intent intent = new Intent(getActivity(), DoctorListActivity.class);  
  3. intent.putExtra("data", doctorSearchPojo);  
  4. getActivity().startActivity(intent);  
And from the Second Activity you can get the data using the following code.
  1. try {  
  2.     doctorSearchPojo = getIntent().getParcelableExtra("data");  
  3. catch (Exception ex) {}  
Now, you can get the member data using the getter methods.