遇到Android onActivityResult()不執行的問題

      之前在使用startActivityForResult()的時候,一直都很是順利,今天卻發現出現了一個奇怪的狀況。 html

      問題描述以下: android

      一、我在執行完startActivityForResult()函數後,尚未啓動目標Activity,當前activity中的onActivityResult()方法卻先執行了。 函數

      二、我在執行完目標Activity,返回的時候,卻發現onActivityResult()方法不執行。 測試

      就那麼幾行代碼,找了很久沒有找到緣由。通過搜索一番,發現問題出在Activity的launchMode上。通過測試發現原來在調用startActivityForResult的時候,目標Activity的啓動模式只能是launchMode="standard" 和 「singleTop」模式,不能是其餘2種模式。 this

去查看android文檔,發現有以下描述: htm

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such asACTION_MAINorACTION_VIEW), you may not get the result when you expect. For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result. 文檔

咱們在使用singleTask或者singleInstance的時候,會新起一個task,這個時候咱們在返回的時候就會返回一個cancel結果。同時從Locat中會看到以下警告: get

Activity is launching as a new task, so cancelling activity result.
相關文章
相關標籤/搜索