覆盖活动对象的onActivityResult从另一个类对象、onActivityResult

2023-09-12 22:27:16 作者:酱子很可爱

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

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.

 
精彩推荐
图片推荐