How to Pass an Object from One Activity to Another on Android

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 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 send an object from one Android Activity to another using Intents?

the most easiest solution i found is..
to create a class with static data members with getters setters.

set from one activity and get from another activity that object.

activity A

mytestclass.staticfunctionSet("","",""..etc.);

activity b

mytestclass obj= mytestclass.staticfunctionGet();

Pass Objects between Activities

Suppose there is a data object class named StudentDataObject having some data types.

StudentDataObject studentDataObject = new StudentDataObject();
Gson gson = new Gson();
String studentDataObjectAsAString = gson.toJson(studentDataObject);

Now we are passing it from one activity to another activity using intent.

Intent intent = new Intent(FromActivity.this, ToActivity.class);
intent.putExtra("MyStudentObjectAsString", studentDataObjectAsAString);
startActivity(intent);

Now we are in new activity, we get that object here using following line.

Gson gson = new Gson();
String studentDataObjectAsAString = getIntent().getStringExtra("MyStudentObjectAsString");
StudentDataObject studentDataObject = gson.fromJson(studentDataObjectAsAString, StudentDataObject.class);

Activity itself know where from I am called, so we can directly write getIntent() method.

Here we only need to add one dependency of GSON we can add it using following line in build.gradle file.

compile 'com.google.code.gson:gson:2.6.2'

And one thing is that implement StudentDataObject as a Parcelable and if showing error then just press alt+Enter and implement methods.
Try this once, Hope it will work.

Sample Example for StudentDataObject should be like :-

  public class StudentDataObject implements Parcelable {
// fields
//empty constructor
//parameterised constructor
//getters and setters
//toString method
//last implement some Parcelable methods
}

Passing ArrayObject from one activity to another

Though you can pass arraylist from one activity to other using serialize method, i feel this is not good way.

Instead you create one Singleton class and setter and getter methods to set and get arraylist.

Initialize singleton object in first activity and set arraylist value.

You can get the singleton class instance in other activity and get the arraylist which is already set by first activity.

Ex:

public class UtilityClass {

private static UtilityClass instance;

private ArrayList list;

public ArrayList getList() {
return list;
}

public void setList(ArrayList list) {
this.list = list;
}

private UtilityClass(){}

public static UtilityClass getInstance(){
if(instance == null){
instance = new UtilityClass();
}
return instance;
}
}

You can set from first activity like below,

UtilityClass.getInstance().setList(list);

Get this from second activity like below,

UtilityClass.getInstance().getList();

How to pass object list from one activity to another in android?

You can send arralist of objects as below

intent.putParcelableArrayListExtra("some_key", (ArrayList<TYPE>) list);
startActivity(intent);

Retrieve it

ArrayList<TYPE> list = (ArrayList<TYPE>)getIntent().getParcelableArrayListExtra("some_key");

how to pass object from one activity to another activity in android Kotlin?

The key you are providing for storing your pojo into the Intent is not the same key you are using to read it (you are storing with "Obj" and reading with "obj").

When you read the intent in your Main2Activity, there is nothing stored with that key, so you return null. And, obviously, you can't cast null to MyPojo.

To avoid these problems, you should always declare a constant for the keys of your intents:

const val ARG_POJO = "argPojo"

And use it to store and read from them:

Store:

intent.putExtra(ARG_POJO, object1)

Read:

val obj: MyPojo = intent.getSerializableExtra(ARG_POJO) as MyPojo


Related Topics



Leave a reply



Submit