startActivityForResult小坑
在開啟一個Activity並希望返回特定資料的時候,會用到startActivityForResult方法:
<span style="font-family: Arial, Helvetica, sans-serif;">public void startActivityForResult(Intent intent, int requestCode) {</span>
startActivityForResult(intent, requestCode, null);
}
就是startActivityForResult方法中,跟進看一下注釋:
/**
* Launch an activity for which you would like a result when it finished.
* When this activity exits, your
* onActivityResult() method will be called with the given requestCode.
* Using a negative requestCode is the same as calling
* {@link #startActivity} (the activity is not launched as a sub-activity).
*
* <p>Note that this method should only be used with Intent protocols
* that are defined to return a result. In other protocols (such as
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect. <span style="color:#ff0000;">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.</span>
*
* <p>As a special case, if you call startActivityForResult() with a requestCode
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
* activity, then your window will not be displayed until a result is
* returned back from the started activity. This is to avoid visible
* flickering when redirecting to another activity.
*
* <p>This method throws {@link android.content.ActivityNotFoundException}
* if there was no Activity found to run the given Intent.
*
* @param intent The intent to start.
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
* Context.startActivity(Intent, Bundle)} for more details.
*
* @throws android.content.ActivityNotFoundException
*
* @see #startActivity
*/
注意紅色字型標出的,如果你是singleTask的啟動模式,那麼你就會在startActivityForResult方法呼叫之後立即接受到一個RESULT_CANCEL的結果,你自己定義的資料並不會接收到,在這裡要注意一下。
相關文章
- Fragment中呼叫startActivityForResult的那些坑Fragment
- 關於startActivityForResult
- Activity的startActivityForResult
- 小程式踩坑填坑
- startActivityForResult()和onSaveInstanceState()用法
- 有來有去 startActivityForResult()
- startActivityForResult和setResult詳解
- android StartActivityForResult()方法詳解Android
- 小程式踩坑
- 小程式實踐小坑小結(一)
- 小程式花式填坑
- 小程式 遇到的坑
- git 爬坑小結Git
- Python 小入坑Python
- 小程式踩坑(2)
- 微信小程式 跳坑微信小程式
- [小程式] mpVue 踩坑Vue
- iOS 小坑總結iOS
- Activity回傳資料方法startActivityForResult onActivityResult
- 小程式的填坑小技巧之CanvasCanvas
- 小程式專案之填坑小記
- swiper.js loop 小坑JSOOP
- mpvue小程式踩坑之旅Vue
- Robot Framework 小坑記錄Framework
- 小程式填坑實錄
- 微信小程式填坑心路微信小程式
- 小程式踩坑系列一
- activity和fragment中startactivityforresult方法的區別Fragment
- Android startActivityForResult()廢棄了,代替方案案例Android
- 老前端出坑小程式(一)前端
- paint.getTextWidths的小坑AI
- 微信小程式爬過的坑微信小程式
- Webpack4-eslint小坑WebEsLint
- 微信小程式填坑清單微信小程式
- php之 trim ltrim rtrim 小坑PHP
- 微信小程式踩坑指南【一】微信小程式
- 小程式使用Picker遇到的坑
- 小程式開發入坑之旅