siutsin
siutsin

Reputation: 1624

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?

Upvotes: 0

Views: 1170

Answers (1)

Piovezan
Piovezan

Reputation: 3223

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.

Upvotes: 2

Related Questions