简体   繁体   中英

Override Activity object's onActivityResult from another class

I passed an activity object to some class:

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

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);
}

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM