android奇怪的問題: onActivityResult在onCreate之前就調(diào)用了

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的模式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容