简体   繁体   中英

How to pass an origin object(NOT COPY) from one activity to another on Android

How to pass origin object from one activity to another on Android?

For sure, we can serialized (Serializable, Parcelable, to/from JSON) and pass a copy of the object's data and a new object having the same data could be created; but it will NOT have the same references.

Here is some solution:

Code for the first activity:

final Object objSent = new Object();
final Bundle bundle = new Bundle();
bundle.putBinder(OBJECT_KEY, new ObjectWrapperForBinder(objSent));
startActivity(new Intent(this, SecondActivity.class).putExtras(bundle));        

Code for the second activity:

final Object objReceived = ((ObjectWrapperForBinder)getIntent().getExtras().getBinder(OBJECT_KEY)).getData();

but the minimum API Level required 18

Are there other ways?

It depends on your goals.
If you're going to work inside one process, you can just simply use Singleton pattern. But if you want to pass your real object to, for example Service , which has attribute android:isolatedProcess="true" :

<service
    android:name=".service.SomeService"
    android:enabled="true"
    android:exported="false"
    android:isolatedProcess="true"/>

Then you can't pass your origin object to another process.

You can't. Apps in one process cannot hold objects from another process, let alone "invoke operations" upon them.

Source: Passing Objects in IPC using Messenger in Android

So, as I can see you are're trying to pass your origin object using Intent mechanism. In my opinion, it's impossible to do it through the Intent 's, because Bundle , which contained in Intent doesn't store references to real objects. So, it's impossible.

In such case, EventBus will be a great choice.It's something like global Subscribe-Observer and it can help to pass origin object between any objects.

Do this in the activity where you want to pass an object:

EventBus.getDefault().register(this);
EventBus.getDefault().post(new MessageEvent());

Do this in the activity where you want to receive the object:

First,register the activity:

EventBus.getDefault().register(this);

Second,define an method with @Subscriber to receive the object:

@Subscribe
public void onMessageEvent(MessageEvent event) 

Hope this will help.

SOLUTION: BundleCompat allows to use putBinder/getBinder for all Android versions.

ActivityA:

final Bundle bundle = new Bundle();
BundleCompat.putBinder(bundle,KEY, new ObjectWrapperForBinder(callback));
intent.putExtras(bundle);

ActivityB:

object = ((ObjectWrapperForBinder)BundleCompat.getBinder(intent.getExtras(),KEY)).getData();

Sample object should be Parcelable.

SampleObject objSent=new SampleObject();
Intent intent=new Intent(mContext,NewActivity.class);
intent.putExtra(objSent);
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM