【发布时间】:2014-01-01 16:44:16
【问题描述】:
我想知道当我们从第二个活动调用类 OtherClass 时,如何发送第一个活动“MainActivity”的引用。 MainActivity 以 Intent 调用 SecondActivity,SecondActivity 必须将 MainActivity 的引用发送给 Otherclass,以便 OtherClass 将最终结果发送给 MainActivity,如下所示:
public class ActA extends Activity implements OtherClass.MyInterface {
//...
//actA sends Intent to actB
}
public class ActB extends Activity {
//send interface actA to otherClass
ActB actB = new otherClass( (OtherClass.MyInterface)ActA.class );//wrong: cannot cast from Class<ActA> to myInterface
//or : actB = new ActB(arg1, ActA.class);//wrong: ask me to change the constructor of Otherclass
//constructor of otherclass
public OtherClass( OtherClass.MyInterface resultReceiver)
//in OtherClass
public interface MyInterface {
public void myFunc();
}
您知道如何解决这些错误吗?
【问题讨论】:
-
您不应将直接实例传递给其他活动。还有其他方法。你的目的是什么
-
你想在这里做什么。为什么需要在ActivityB中引用activityA??
-
不要这样做,这是造成巨大内存泄漏的一种方式。
-
我的解释不是很清楚,抱歉。 OtherClass 需要将最终结果发送到 MainActivity(我编辑了我的帖子),我可以尝试使用 MainActivity 捕获的广播,但代码是由其他人编写的,并允许添加接口类型的参数。在我的情况下,这是 MainActivity,但它不起作用。
标签: android interface android-activity