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

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?

The correct chain of events is the following:
You call requestPermissions in the Activity's onCreate
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
the request of the permission generates a dialog, which fires
onPause on the Activity, because it is no more in foreground position.
The activity at this moment is paused and you can see a dialog
asking to allow or deny the permission.
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.

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

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

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.

Related

How to run code when app is re-entered after going to home (Android)

I want to run some code after I hit my home button and the re-open the app. How do I run code when the app is re-opened (Not After Being Killed) in android-studio?
you can use onStop() method or onDestroy() , if you will close totally that Activity use onDestroy() and if you just will put it in background you can use onStop()
Doc for onDestroy()
Perform any final cleanup before an activity is destroyed. This can
happen either because the activity is finishing (someone called
finish() on it, or because the system is temporarily destroying this
instance of the activity to save space. You can distinguish between
these two scenarios with the isFinishing() method.
Doc for onStop()
Called when you are no longer visible to the user. You will next
receive either onRestart(), onDestroy(), or nothing, depending on
later user activity.
i saw you edited your question, look at the lifecycle
Like sagar says, you can use onResume() in order to recover your current Activity
You can perform the action in onResume() method. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Do note that there is no differentiation between Activity coming from background to foreground or Activity created from scratch. onResume() will be called whenever app enters foreground state.
In order to differentiate it in onResume() you need to maintain a boolean flag. You can set the flag in onStop() and check it in onResume().
This approach will only work, if the OS hasn't killed the process, hosting your Activity, due to memory constraints. In this case your Activity will be recreated.
You can use onRestart() since when you left your activity to go home it goes into onStop() and after that when you launch it system will call onRestart() the onStart().
for more info refer following ans:
https://stackoverflow.com/a/35476531/7271231

Confusion between onPause() onStop() onResume()

I am developing an Android app in which I want to check if the user has minimized the application or just come from another activity.
In detail, if the user have started another app, went to the home screen or locked the screen, I want to show the activity where the user will enter the password to access the app. But where or how to check this exactly?
https://developer.android.com/guide/components/activities/activity-lifecycle.html
I was trying onResume() but according to documentation onResume() can be fired if the user’s navigating to another activity and coming back.
I'm not very clear on what you are trying to achieve.
The life cycle diagram is quite clear if you are wondering which lifecycle method it would hit when something happens.
Basically, it's the same to minimise the app and go to another activity. But if you are referring to coming from another activity in your own app, you can distinguish your own activity by adding extra information to the intent you use.
Basically, it's like this:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra(key,value);
startActivity(intent);
And in your SecondActivity, you can always retrieve that data like this:
Bundle bundle = getIntent().getExtras();
if ( bundle != null && bundle.containsKey(key) ) {
value = bundle.getInt(key); // not nessecarily getInt(), you should use according to your value type
// use the value to tell if it is from your own app
} else {
// it is not from your own app
}
You can use this mechanism combined with the lifecycle methods. For example, if you use the latter code in your onCreate() method, then whenever the Activity is created, if will check who creates it, which sounds like your what you might want.
As soon as your activity becomes visible it will call OnStart() and as soon as it is ready for the interaction(such as touch ,click etc event). it calls onResume, at this stage your app is running and it is completely in foreground. When your activity start another activity or a dialog box then it calls onPause it means activity is visible but user can not interact with the Activity UI. in case we start another Activity which completely hides the previous activity then its onStop method is called
onPause: Called when another activity comes into the foreground.
onStop: Called when that other activity is completely visible.
onResume: Called when your activity is navigated back to from the onPause state.
Maybe your app was already in the onStop state, so then it would call onRestart.

When is finish() executed?

I was searching, when onDestroy is executed on an android application, so I found that it is executed when the device is low of resources(RAM, CPU) and when finish() is called by the user.
For example, when I press the back button to return from an activity to previous activity, finish() is executed?
Or in what situation is finish() executed?
Thanks.
finish is not a callback method like onDestroy, so it won't be called by the system. the developer can call it if he needs it .
If you press back button then it will call OnBackPressed method. you can call finish on activity whenever you need to finish the activity.
e.g. activity.finish(). it will complete the Activity lifecycle.

Activity receive onResume event before another disappear

I am programming an app with the use of OpenGL and SurfaceView.
I need to detect when an activity finishes and I return to my activity.
I need it because I don't want to respond to touch and key events until my activity is shown.
Now I detect it with onResume, but onResume is called before the finishing activity disappears.
I need to enable these events when my activity is being shown (mostly because of onBackPressed).
Anyone with a solution?
EDIT:
find some information about it, so when the activity is doing something in its onStop/onDestroy method, then it is after onResume of my activity was called -> my problem
http://developer.android.com/guide/components/activities.html#CoordinatingActivities
EDIT2:
onStop and onDestroy are called when activity is not showing
EDIT3: found that it is caused because of new thread doing work in onPause, still no solution
Weird because onResume should be called when your activity is visible.
You could activate your touch controls with a small delay using a handler.

Android: When to end class with finish()?

I often see examples of classes which end with finish(), but definitely not always. My question is when should you end a class with finish()? And what does it do exactly, what is the difference between ending a class with the back button and ending it with finish()?
Thanks in advance!
finish() can be called to kill (destroy) an Activity instance. If you don't need to close your Activity manual, which is true in many cases, you don't need to call this method.
But if you require a button somewhere in your activity that says "close", then you should use this method. But in general the back button behavior in Android will handle things like this.
The back button does not actually finish your activity, finish() calls the onDestory() method right away, while the back button does not.
When the back button is pressed, the onStop() method is called, but the onDestory() method call might be delayed by the system, this so that the Activity can be resumed by the system which is cheaper (in resources) than a full restart.
Lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Finish():
http://developer.android.com/reference/android/app/Activity.html#finish()
You finish Activity class with finish() method when you need to end the Activity immediatly at specific point. If you press back button, you will go through the lifecycle (onPause(), onStop() then onDestroy()) automatically.
One reason to use finish is when you have started the activity with
void android.app.Activity.startActivityForResult(Intent intent, int requestCode)
In this case you dont go back to the Activity which started it with another startActivity but with finish
Back button takes you to the last activity in the stack whereas finish() destroys the current activity. You can call finish() on an activity for which you don't want the user return to. Example: LoginActivity
public void finish ()
Call this when your activity is done and should be
closed. The ActivityResult is propagated back to whoever launched you
via onActivityResult().
finish() is called to kill your activity instance after you did what you have to do with the activity. After that, you have no use with the activity. So simply call finish() and it will kill your activity.
If you press back button, it will call finish() automatically to kill the activity. It will go through the activity lifecycle(onPause(), onStop() and finally onDestroy())
The one reason with calling finish() is that, if you don't call it, when you navigate through activities, all the activities will be added to the backstack. So when you go back, you have to come back through these activities. So for e.g when you click back button, it simply kills the activity and it will be removed from the backstack.

Categories

Resources