Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 794
mike_x_ Avatar asked May 04 '17 08:05

mike_x_


People also ask

Does onResume get called after onCreate?

onResume() will never be called before onCreate() . Save this answer. Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

What is the relationship between onStart () and onResume ()?

onStart() -> called when the activity becomes visible, but might not be in the foreground (e.g. an AlertFragment is on top or any other possible use case). onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Save this answer.

What is onResume ()?

4. onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.

What is the use of onResume method in Android?

onStart() , onStop() , onRestart() : Called back when the Activity is starting, stopping and re-starting. onPause() , onResume() : Called when the Activity is leaving the foreground and back to the foreground, can be used to release resources or initialize states.


3 Answers

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.

like image 56
Simone Avatar answered Nov 03 '22 11:11

Simone


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

like image 30
默默磊OK Avatar answered Nov 03 '22 10:11

默默磊OK


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

like image 2
patilmandar2007 Avatar answered Nov 03 '22 09:11

patilmandar2007