Question

Is there any way I can pass a Parcelable (via Intent) that has a View.OnClickListener on it?

Basically I want to be able to start a common Actvity from anywhere and pass over the OnClickListener for a button which is defined on the Activity.

Alternatively I don't need to send the OnClickListener, it can just be a custom interface implementation that a fixed OnClickListener can callback to, the problem is I can't see any way to parcel anything like a callback (a method that can later be called)

I had tried the following

  destParcel.writeValue(mListener)

but it threw the following

  java.lang.RuntimeException: Parcel: unable to marshal value

So is there anyway to write a parcellable callback?

Was it helpful?

Solution

Is there any way I can pass a Parcelable (via Intent) that has a View.OnClickListener on it?

Not in any meaningful fashion. Parceling is copy-by-value, not copy-by-reference, so it is not the same object on the receiving side as on the sending side.

Basically I want to be able to start a common Actvity from anywhere and pass over the OnClickListener for a button which is defined on the Activity.

Use an event bus (Square's Otto, greenrobot's EventBus, LocalBroadcastManager, etc.).

Or, use a PendingIntent.

Or, use a Messenger.

Or, use a ResultReceiver, which is the closest thing there is to a "parcellable callback".

OTHER TIPS

You'll have to create a class that extends View.OnClickListener and implement Parcelable interface. If you have multiple listeners they can implement a common interface with the method you want to call.

This will work as long as you can save and restore all class members in/to a Parcel. However, if your listener wants to reference View objects this will not work and wouldn't make much sense to send it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top