Passing data of a non-primitive type between activities in android
You have a few options:
- You could wrap the more complex structure in a class that implements the
Parcelable
interface, which can be stored in an extra - You could wrap the more complex structure in a class that implements the
Serializable
interface, which can be stored in an extra - You use static data members to pass stuff around, since they are all in the same process
- You use external storage (file, database,
SharedPreferences
) - As the person who just posted noted, use a common component, such as a custom
Application
or a localService
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
How to Size an Android View Based on Its Parent's Dimensions
How to Add an Image on Edittext
Android Studio Error Installing Gradle
Dynamic Form with Repeating Form
Making Data Persistent in Android
Get Only Email Address from Contact List Android
How to Integrate Eclipse with Gradle in Android Project
Detect the Status of Two Sim Cards in a Dual-Sim Android Phone
Android Studio - Android Emulator Wifi Connected with No Internet
Android Lollipop, Appcompat Actionbar Custom View Doesn't Take Up Whole Screen Width
Implementation of Onscrolllistener to Detect the End of Scrolling in a Listview
Scan_Results_Available_Action Return Empty List in Android 6.0
Android: Using Linear Gradient as Background Looks Banded
Setuservisiblehint Called Before Oncreateview in Fragment
How to Securely Store Credentials (Password) in Android Application