0

I need to pass Objects between two different processes, I read all the materials about it but I still have some questions

1.I can pass objects between different processes through AIDL, but its complicated and messy, I also read about he Messenger

to transfer messages, but cant I pass with it an Object that implements Parcelable like that?

Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);

handler.sendMessage(msg);

And in the Handler to extract it? if yes so what is the advance of AIDL over Messenger...only the multithreading issue?

2.To work across different processes I can with Messenger and AIDL, but cant I use Bundle in Activities without all those services like some Android app work today that can get objects as input:

intent.putExtra("MyParcelableObject", object);
AlexKulr
  • 69
  • 11

2 Answers2

1

First of all implement java.io.Serializable interface in your POJO class, like this:

public class MyPojo implements Serializable {

}

Secondly, in your MainActivity where you want to pass objects write something like this:

Bundle bundle = new Bundle();
bundle.putSerializable("",""); // to hold list
bundle.putInt("",""); // to hold int
bundle.putString("", ""); // to hold String
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(bundle);
startActivity(intent);

Finally, in your SecondActivity you can get/read data, in the following way

Bundle bundle = getIntent().getExtras();
ArrayList<MyPojo> arrayList= (ArrayList<MyPojo>) bundle.getSerializable("list");
int myPosition = bundle.getInt("position");
String strValue = bundle.getString("myString");
Mysterious
  • 173
  • 10
0

If you just want to pass data between components residing in two different processes you can use the similar semantics as the ones used to share objects between two activities.For example, communicating between activity in Process 1 to a service in Process 2 can happen in following manner:

From the activity in process 1

 Intent mIntent = new Intent(this, MyService.class);
    Bundle extras = mIntent.getExtras();
    extras.putString(key, value); 
    startService(mIntent);

In the service of process 2

public int onStartCommand (Intent intent, int flags, int startId)
{
     super.onStartCommand(intent, flags, startId);

     Bundle bundle = intent.getExtra();

}

Also while sharing objects use Parcelable instead of Serializable as they are more lightweight.

rupesh jain
  • 3,410
  • 1
  • 14
  • 22