在意图之间在活动之间传递接口-接口无法序列化或可打包 [英] Pass interface between activities in intent - interface fails to be Serializable or Parcelable

查看:61
本文介绍了在意图之间在活动之间传递接口-接口无法序列化或可打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将界面从第一活动传递到第二活动.

I want to pass an interface from 1st activity to 2nd activity.

我想从第二个活动的接口中启动方法,这会影响第一个活动.

I want to initiate methods from the interface from the 2nd activity which will affect the 1st activity.

我很清楚,如果不使用 onActivityResult,这是非常过分的机制,可能不是很好的编程方法,但请跟我一起滚动.

I'm well aware that it's very overkilling not using the onActivityResult mechanism, and that it might not be good programming, but roll with me please.

这是问题所在-我的界面无法实现可序列化/可拆分,因为接口无法实现其他类

Here's the issue - my interface can't implement Serializable / Parcelable since interface can't implement another class.

这是我的界面:

public interface ITest {
     void onSuccess(String text);
}

但是,我无法通过此界面开始我的活动,因为它不是 Parcelable.

But, i can't start my activity with this interface since it's not Parcelable.

intent.putExtra("testInterface", new ITest() {
    @Override 
    void onSuccess(String text) {
    }
}

很明显,我收到一个编译错误:无法解析方法'putExtra(java.lang.String,ITest)'

Obviously, i receive a compilation error : Cannot resolve method 'putExtra(java.lang.String, ITest)'

推荐答案

您不能通过接口".接口"是抽象的东西.您要传递的是该接口的具体实现,即:一个对象(恰好实现您的接口).实例化接口"时(在您的示例中,如下所示:

You cannot "pass an interface". An "interface" is an abstract thing. What you want to pass is a concrete implementation of that interface, ie: an object (that happens to implement your interface). When you instantiate your "interface" (in your example, like this:

intent.putExtra("testInterface", new ITest() {
    @Override 
    void onSuccess(String text) {
    }
}

您实际上正在创建实现接口 ITest 的匿名类的实例.要在 Intent 中传递此信息,您需要使此类还实现 Parcelable Serializable .

you are actually creating an instance of an anonymous class that implements the interface ITest. To pass this in an Intent you would need to make this class also implement Parcelable or Serializable.

但是,即使您这样做了,也无法解决您的问题.您需要了解的是,您无法通过将对象(实例)放在" Intent "中作为"extras"来传递对象.当您这样做时,Android实际上会先序列化然后反序列化对象,以便最终得到2个对象,其中一个是原始对象的序列化/反序列化副本.

However, even if you did that, it would not solve your problem. What you need to understand is that you can't pass objects (instances) by putting them as "extras" in an Intent. When you do that, Android actually serializes and then deserializes the object so that you end up with 2 objects, one is a serialized/deserialized copy of the original.

如果您希望 ActivityB ActivityA 通信,则需要使用另一种技术.尝试以下方法之一:

If you want ActivityB to communicate with ActivityA, you will need to use another technique. Try one of these:

  • ActivityB 发送广播 Intent ActivityA 监听
  • ActivityA 使用 startActivityForResult()启动 ActivityB ,而 ActivityB 将数据发送回 ActivityA setResult()
  • 使用公共静态(即:全局)变量进行通信
  • 以共享首选项,文件或数据库存储数据
  • ActivityB sends a broadcast Intent, which ActivityA listens for
  • ActivityA starts ActivityB using startActivityForResult() and ActivityB sends data back to ActivityA using setResult()
  • Use public static (ie: global) variables to communicate
  • Store data in shared preferences, or a file, or a database

您真正需要了解的是,在某些情况下,可能会发生以下情况:

What you really need to understand is that, under certain conditions, the following can occur:

  • 您的应用正在堆栈中运行 ActivityA ,并在堆栈顶部运行 ActivityB
  • 用户按下HOME
  • Android将您的任务移至后台,并最终终止了托管过程
  • 用户返回到您的应用程序(通过重新启动它或从最近任务"列表中选择它
  • Android为您的应用程序创建一个新进程,并实例化 ActivityB ,然后调用 onCreate() onStart() ActivityB 的onResume().
  • your app is running with ActivityA in the stack and ActivityB on the top of the stack
  • user presses HOME
  • Android moves your task to the background and eventually kills the hosting process
  • User returns to your app (by starting it again or selecting it from "recent task" list
  • Android creates a new process for your app, and instantiates ActivityB, then calls onCreate(), onStart() and onResume() of ActivityB.

在这种情况下,不再有 ActivityA 的实例.那个实例已经死了.因此, ActivityB 无法与 ActivityA 通信,因为它不再存在.

In this case, there is no instance of ActivityA anymore. That instance is dead. So ActivityB cannot communicate with ActivityA because it no longer exists.

这篇关于在意图之间在活动之间传递接口-接口无法序列化或可打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆