Passing Data of a Non-Primitive Type Between Activities in Android

Passing data of a non-primitive type between activities in android

You have a few options:

  1. You could wrap the more complex structure in a class that implements the Parcelable interface, which can be stored in an extra
  2. You could wrap the more complex structure in a class that implements the Serializable interface, which can be stored in an extra
  3. You use static data members to pass stuff around, since they are all in the same process
  4. You use external storage (file, database, SharedPreferences)
  5. As the person who just posted noted, use a common component, such as a custom Application or a local Service

What you do not want to do is pass big stuff via extras. For example, if you are creating an application that grabs pictures off the camera, you do not want to pass those in extras -- use a static data member (icky as that sounds). Intents are designed to work cross-process, which means there is some amount of data copying that goes on, which you want to avoid when it is not necessary for big stuff.

How do I pass an object from one activity to another on Android?

When you are creating an object of intent, you can take advantage of following two methods
for passing objects between two activities.

putParcelable

putSerializable

You can have your class implement either Parcelable or Serializable. Then you can pass around your custom classes across activities. I have found this very useful.

Here is a small snippet of code I am using

CustomListing currentListing = new CustomListing();
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.CUSTOM_LISTING, currentListing);
i.putExtras(b);
i.setClass(this, SearchDetailsActivity.class);
startActivity(i);

And in newly started activity code will be something like this...

Bundle b = this.getIntent().getExtras();
if (b != null)
mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);

How to pass an object from one activity to another on Android

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.

Actual Code:

In Your Custom Model/Object Class:

public class YourClass implements Serializable {

At other class where using the Custom Model/Class:

//To pass:
intent.putExtra("KEY_NAME", myObject);

myObject is of type "YourClass".
Then to retrieve from another activity, use getSerializableExtra
get the object using same Key name. And typecast to YourClass is needed:

// To retrieve object in second Activity
myObject = (YourClass) getIntent().getSerializableExtra("KEY_NAME");

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {

public MainClass() {}

public static class ChildClass implements Serializable {

public ChildClass() {}
}
}

How to pass data between activity efficiently?

You can use a singleton class to store objects and retrieve them in other activity by getting instance of that singleton class.

Ref: http://developer.android.com/guide/faq/framework.html#3



Related Topics



Leave a reply



Submit