有什么方法可以传递一个带有 View.OnClickListener 的 Parcelable(通过 Intent)?
基本上,我希望能够从任何地方启动一个通用的 Actvity,并为在 Activity 上定义的按钮传递 OnClickListener。
或者我不需要发送 OnClickListener,它可以只是一个自定义接口(interface)实现,一个固定的 OnClickListener 可以回调,问题是我看不到任何方式来打包任何东西,比如回调(一种方法以后可以调用)
我试过以下方法
destParcel.writeValue(mListener)
但它抛出了以下内容
java.lang.RuntimeException: Parcel: unable to marshal value
那么有没有办法写一个 parcellable 回调?
最佳答案
Is there any way I can pass a Parcelable (via Intent) that has a View.OnClickListener on it?
没有任何有意义的方式。 Parceling 是按值复制,而不是按引用复制,因此它在接收端和发送端不是同一个对象。
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.
使用事件总线(Square 的 Otto、greenrobot 的 EventBus、LocalBroadcastManager
等)。
或者,使用 PendingIntent
。
或者,使用 Messenger
。
或者,使用 ResultReceiver
,这是最接近“可分包回调”的东西。
关于Android 包裹并传递一个带有监听器的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433347/