简体   繁体   中英

onActivityResult gets called before onDestroy

Inside MainActivity.java I make the following call:

Intent activity = new Intent(this, CameraDetectionActivity.class);
startActivityForResult(activity, request);

And at some point inside CameraDetectionActivity I run the following 2 lines (all happens in the GUI thread):

setResult(Activity.RESULT_OK);
finish();

I would expect onDestroy to get called before onActivityResult in MainActivity but they are called in the opposite order. Any idea why this is happening?

onDestroy is called at some point later, and that may be arbitrarily long. We want to resume the next activity as quickly as possible so the UI is in there, and then take care of stopping and destroying previous activities only once the UI has switched.

Put attention pass your data to parent activity before calling super.onDestroy();

@Override
protected void onDestroy() {
    Intent intent = new Intent();
    setResult(RESULT_OK, intent);
    finish();
    super.onDestroy();
}

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