覆盖活动对象的onActivityResult从另一个类 [英] Override Activity object's onActivityResult from another class

查看:201
本文介绍了覆盖活动对象的onActivityResult从另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个活动对象的一些类:

I passed an activity object to some class:

// SomeClass.java constructor
public SomeClass(Activity activity) {
    this.mActivity = activity;
}

我需要使用活动对象的startActivityForResult:

I need to use the startActivityForResult of the activity object:

private void goToAnotherActivity() {
    Intent intent = new Intent(this.mActivity, AnotherActivity.class);
    this.mActivity.startActivityForResult(intent, INTENT_KEY);
}

是否有可能重写this.mActivity的onActivityResult从SomeClass.java?

Is it possible to override the this.mActivity's onActivityResult from SomeClass.java?

推荐答案

您并不需要显式调用 onActivityResult()。只是覆盖在活动传递到SomeClass的,当 AnotherActivity 结束后它会被执行。如果需要,你可以创建一个 BaseActivity 将覆盖 onActivityResult()并有你想要传递活动到SomeClass的延长 BaseActivity

You don't need to explicitly call onActivityResult(). Just override it in the Activity passed to SomeClass and it will be executed when AnotherActivity is finished. If you need to, you can create a BaseActivity which overrides onActivityResult() and have the activities you intend to pass on to SomeClass extend BaseActivity.

另一种方法是使用Java反射/仪表API覆盖上即时的方法 mActivity ,万一真的是有必要的。

Another approach would be to use the Java reflection/instrumentation API to override the method in mActivity on-the-fly, in case it is really necessary.

这篇关于覆盖活动对象的onActivityResult从另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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