Is onResume() called after onRequestPermissionsResult() in Android?

本秂侑毒 提交于 2019-12-11 00:09:11

问题


I have an activity and i have put checkSelfPermission and requestPermissions methods in onCreate method.

I also have an onRequestPermissionsResult method outside of onCreate.

Finally i have an onResume method, too.

Which one is called first, onResume or onRequestPermissionsResult?


回答1:


The correct chain of events is the following:

  1. You call requestPermissions in the Activity's onCreate

  2. requestPermissions start running in another thread, because it is designed to not block the UI thread. So your activity goes through onStart and then onResume

  3. the request of the permission generates a dialog, which fires onPause on the Activity, because it is no more in foreground position.

  4. The activity at this moment is paused and you can see a dialog asking to allow or deny the permission.

  5. You make your choice, the dialog gets resolved and onResume is called on the Activity.

Also notice that the onPause is fired by the dialog always after onStart and onResume of the Activity, no matter how long it takes to execute the code in them.

And now you can also see why you shouldn't put requestPermissions in onResume.




回答2:


The first one is onRequestPermissionsResult !

I have destroy some object on onPause(), and It will be recreate on onResume(), but I find my onRequestPermissionsResult() operate some destroyed object and caused NullPointEx




回答3:


onResume() will be called first during the launch of your Activity as onRequestPermissionsResult(...) will only be called after user accepts or denies permission to application in Permission request dialog. But onResume again gets called after onRequestPermissionsResult(...) is called to allow your activity to take in account the user choice (granted or denied permission) and execute the code accordingly




回答4:


onCreate called first and only once when activity launched first time.

onResume called directly after onCreate or when activity return after pause.

And onRequestPermissionsResult called after user confirm permission.

Conclusion:onResume is called before onRequestPermissionsResult.

You can read about Activity LifeCycle https://developer.android.com/guide/components/activities/activity-lifecycle.html

Please mark as answered if it help.



来源:https://stackoverflow.com/questions/43777182/is-onresume-called-after-onrequestpermissionsresult-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!