When is finish() executed? - java

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.

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

Activity Create and resume again and again in Nougat Version

I am navigating from Activity A to Activity B without finishing Activity A (because i want to go back on it and have some variables values).
In Activity B I launch camera and save captured image but the problem is, after capture camera the Activity A is re-create and resumed, causing re initialization of my variables.
How to stop it?
Note: The problem occurs only in Nougat Version.
The thing you want to know here is about the complete activity life cycle. Basically to summerize them they are
OnCreate, - called when activity is created
OnStart, - called when activity starts
OnREsume, - when activity gets back
OnPause, - when activity is overlapped
OnStop, - when activity is closes
and onDestroy - when finish() is called.
Though you have not mentioned how you started an activity and got back to the same activity, the correct way to get back to previous activity is by calling
finish()
on camera activity.
I guess you have got back to activity A by using
Intent i= new Intent(this, ActvityA.class)
startActivity(i)
which is the the correct way to do.
Just call the finish() when you want to get back to Activity A from B (Here B is in top of A).
Additional
If you want to pass data from activity B to A, just put something called as Intent Extra or Bundle
Activity A is create again and resume, so my variables are initialize again. How to stop it.
Yes that is normal. First rule with Android programming is that your activity can be killed at any time. So to not loose your variables you have to save them at the right moment.
You would do that to override onSaveInstanceState() putting your variables in the bundle.
Then you can retrieve them in onCreate() from the function parameter.

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?
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.

What method call from Back button pressed of Android Activity?

I read along the documentation in this page. And obtaining clear picture of one activity starts in android. Just like this screenshot below:
Here is my case.
Activity -A called Intent to start Activity B-.
And then from Activity -B the user click Back button.
So then it returned to Activity -A.
my question is .... since now we're in that Activity -A.
Does onPaused() method called back or onResumed() method
that is called now?
When ActivityB takes the foreground, Activity A is put into the stopped state (onPause and onStop will be called). When ActivityA comes back, it will get onStart, followed by onResume.

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