There's a bug / feature (?) in Android, which immediately reports result (which has not been set yet) for activity, declared as singleTask (despite the fact that the activity continues to run). If we change launchMode of the parent activity from singleTask to singleTop, everything works as expected - result is reported only after the activity is finished. While this behavior has certain explanation (only one singleTask activity can exist and there can happen multiple waiters for it), this is still a not logical restriction for me.
當(dāng)一個(gè)activity(A) launchMode設(shè)置成singleTask后 前一個(gè)activity(B)如果用startActivityForResult來啟動(dòng)這個(gè)Activity(B)那么 A的onActivityResult會(huì)提前調(diào)用
解決辦法
啟動(dòng)activity A時(shí)設(shè)置flag
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
FLAG_ACTIVITY_CLEAR_TOP
如果在ABCD的堆棧狀態(tài)下,以該標(biāo)識(shí)啟動(dòng)B,則會(huì)銷毀CD,且B也是重新創(chuàng)建的(與singleTask有區(qū)別),如果配合FLAG_ACTIVITY_SINGLE_TOP,則就會(huì)成為singleTask的模式