Android onActivityResult()和onResume()的執(zhí)行順序

問題回顧:

在Android8.0手機(jī)上不能直接安裝Apk,需要權(quán)限申請(qǐng),回調(diào)后執(zhí)行onActivityResult()方法,并彈出對(duì)應(yīng)的dialog,這時(shí)需要判斷當(dāng)前Activity是否處于resume狀態(tài),通過變量來控制,但結(jié)果是這個(gè)dialog并不會(huì)彈出。

問題本質(zhì)

Activity跳轉(zhuǎn)到系統(tǒng)應(yīng)用后回調(diào)onActivityResult() 先執(zhí)行還是onResume()先執(zhí)行?

查看源碼

 /**
     * Called when an activity you launched exits, giving you the requestCode
     * you started it with, the resultCode it returned, and any additional
     * data from it.  The <var>resultCode</var> will be
     * {@link #RESULT_CANCELED} if the activity explicitly returned that,
     * didn't return any result, or crashed during its operation.
     *
     * <p>You will receive this call immediately before onResume() when your
     * activity is re-starting.
     *
     * <p>This method is never invoked if your activity sets
     * {@link android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
     * <code>true</code>.
     *
     * @param requestCode The integer request code originally supplied to
     *                    startActivityForResult(), allowing you to identify who this
     *                    result came from.
     * @param resultCode The integer result code returned by the child activity
     *                   through its setResult().
     * @param data An Intent, which can return result data to the caller
     *               (various data can be attached to Intent "extras").
     *
     * @see #startActivityForResult
     * @see #createPendingResult
     * @see #setResult(int)
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

onActivityResult()注釋中有這么一句話:

You will receive this call immediately before onResume() when your activity is re-starting.

(意思是說當(dāng)你的Activity重新啟動(dòng)時(shí) onActivityResult() 會(huì)在onResume()之前執(zhí)行)

很顯然,如果你的Activity 調(diào)用了 startActivityForResult() 方法啟動(dòng),回調(diào)之后會(huì)先執(zhí)行 onActivityResult() ,然后執(zhí)行onResume()

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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