How Onstop Method work for this demo? - android

I am making a demo to understand back stack and activity life-cycle.
I made:
MainActivity
MainActivityDialog (another activity theme as dialog)
BActivity
I launched this app and Main Activity is shown. Then press a button to show the dialog, then MainActivityDialog is opened. Then I again press button on MainActivityDialog then BActivity is opened. Finally, I pressed the back button.
MainActivity -> MainActivityDialog -> BActivity ---Back---> MainActivityDialog
Here is the log of this app :
My question are:
Why MainActivity get stopped after launching BActivity from MainActivityDialog? Then after BActivity lifecycle method is called, why MainActivityDialog get stopped?
After pressing back button in BActivity, MainActivity starts first then MainActivityDialog starts and then MainActivityDialog resume?

The order of calls to onStop() and onDestroy() on multiple activities is indeterminate.
If you have multiple activities in your activity stack that are no longer visible on screen, Android may call onStop() on them whenever it wants to and in whatever order it wants to. This is only an indication to the activity that it is no longer visible to the user. You cannot rely on the order of onStop() calls to multiple activities.
The same goes for onDestroy(). Android may call onDestroy() on an activity once that activity has finished. If you have multiple finished activities in your task, Android may call onDestroy() on them whenever it wants to and in whatever order it wants to. This is also indeterminate. The call to onDestroy() is just to inform the activity that it is no longer active and that it should release any resources it may have.
There is no guarantee that onStop() or onDestroy() will ever be called. The last lifecycle call that is guaranteed is onPause(). After that, Android can just kill the process without calling any further lifecycle methods.
In your second question you want to know why, after the user presses the BACK button on BActivity, MainActivity starts first followed by MainActivityDialog. The reason is that MainActivity is visible on screen FIRST and then MainActivityDialog is visible on screen on top of MainActivity (because MainActivityDialog is Dialog-themed, it doesn't cover the entire screen and you can see parts of MainActivity underneath it).

Im not a pro at Android, but since nobody has answered yet, i will try my best. I want to help because i also learn android by doing something like you did (using log cat to see activiti's current state) for the first time.
Why MainActivity get stopped after launching BActivity from
MainActivityDialog?
Every time you start a new activity, the old activity will always be paused/stopped.
Then after BActivity lifecycle method is called, why
MainActivityDialog get stopped?
Because now the current active activity is BActivity, in other word : MainActivityDialog is not active/visible hence its stopped/paused.
After pressing back button in BActivity, MainActivity starts first
then MainActivityDialog starts and then MainActivityDialog resume?
Yes, because you started MainActivityDialog from MainActivity, so MainActivity will be restarted first.
Please feel free to comment, im also still learning :)

Related

First Activity not stopped when Second Activity started

As per Android activity life cycle, when activity is no longer visible onStop will get called. But this is not happening, if i exit the second activity quickly.
I have two activities HomeActivity and DetailActivity.
Step 1. Pressing a button on HomeActivity navigates to DetailActivity
Step 2. Back press on DetailActivity navigates to HomeActivity
When navigates to DetailActivity , the onStop of HomeActivity should get called as per activity life cycle, since the DetailActivity gets onStart.
But onStop is not getting called if i back press on DetailActivity quickly.
Activity life cycle if back pressed immediately..
HomeActivity: onPause
DetailActivity: onStart
DetailActivity: onResume
DetailActivity: onPostResume
DetailActivity: onPause
HomeActivity: onResume
HomeActivity: onPostResume
DetailActivity: onStop
Activity life cycle if back pressed after a delay (say few seconds later on DetailActivity)
HomeActivity: onPause
DetailActivity: onStart
DetailActivity: onResume
DetailActivity: onPostResume
HomeActivity: onStop
DetailActivity: onPause
HomeActivity: onStart
HomeActivity: onResume
HomeActivity: onPostResume
DetailActivity: onStop
Doesn't this look like a bug as new Activity is Started, the previous activity is not stopped?
According to Android Docs about stopping and restart of an Activity
The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.
They should update it to say If the user the presses the Back button but not to quickly haha, but I think iheanyi is right in the fact that some other process may have been granted cpu time after the DetailsActivity hit onPostResume and the back button was pressed before the OS could continue with the onStop process of the HomeAcitvity
You've described normal behavior. Here's what https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle says about this:
Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This may
happen either because a new activity is being started, an existing one
is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to
interact with the user, or onDestroy() if this activity is going away.
So, you may be wondering, well, if my new activity is visible, doesn't that mean the previous activity is invisible? Not really. Your new activity may not completely overlap the old one (in which case, part of the old is still visible). This could be especially tricky if the old activity has elements that while "invisible" to the user because they look the same as elements from the new activity, are technically still visible (in that they're not completely blocked by the new).
Visibility aside, you could also inadvertently have lifecycle methods timing issues which lead to this. Let's say your onPause() takes a long time to complete. The other activity could completely hide the old, satisfying the conditions for onStop(), but you transition back before onStop() has a chance to run.
Finally, there are many tasks that need to run on the phone and few processors to execute them. Necessarily, some tasks will have higher priority than others. Think about the code that must execute when your new activity is taking over the foreground - there is no way that onStop() has time to run if the phone is busy executing code for your activity. If that code then triggers onResume, onStop would be skipped altogether.
As you can see from the life cycle diagram, you can transition from onPause() back to onResume() without going through onStop(). Perhaps you should think about the scenarios where something like that can happen (and why) to better understand your particular case.

Tracking the activity life cycle

I have requirement wherein I have to display a lock screen if the user moves out of the application.
Hence, the structure is: Activity A extends Activity B.
Wherein Activity B is the deciding activity : "was application in backgound".
If so it launches the lock activity.
Now, say I am on activity A and receive a phone call. Hence the app gets into the background.
When it resumes I can see the glimpse of Activity A for a fraction of second and then comes the lock activity.
Can there be any solution to avoid that glimpse of Activity A?
You can see the lifecycle of activity from official doc
You are using activity B just to track whether activity is alive. I am not sure if it is necesary.
If activity goes to background onPause() method is called, it means activity is not visible (it might be both screen lock or home button pressed), and onResume() is called when activity is visible again. In Activity A if you override onPause method and launch your lock activity, it should work. (Or set a boolean onPause and launch lock activity on resume(you might see Activiy A though)
Good luck

Life cycle of Android Activity after pressing Back button

I am little confused between the life cycle of two activities.
Suppose I have Activity A and Activity B.
B is called From A i.e A ----> B.
Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still available for B(Active) or B's memory is flushed(Inactive).
The following activity call back methods are called, after pressing back button.
onPause()
onStop()
onDestroy()
The activity is destroyed.
And it recreates when launched again. These are the callback methods when it launches again.
onCreate()
onStart()
onResume()
I know the answer is been accepcted, still if this helps someone I am putting it.
When app is opening for the first time, by clicking the Icon
onCreate()
onStart()
onResume()
When home button is pressed
onPause()
onStop()
when app is again opened by clicking the app icon or launched from recent
onRestart()
onStart()
onResume()
when app is opened and then back button is pressed
onPause()
onStop()
onDestroy()
The onDestroy method is called after back press. Then activity will be popped from the Activity back stack.
From docs:
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
onDestroy() from docs:
The final call you receive before your 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.
Activity B will be destroyed and will no longer remain in memory.
For more information please visit the official documentation for android and have a look at the activity life cycle figure.
Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.
I would suggest to refer following link for activity lifecycle
http://stackoverflow.com/a/8516056/3110609
and following link for launch mode of activity.
www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
After pressing the back button, Activity B will b destroyed. You see, Android Manages Activities like a Stack(an explanation of a stack). Everytime you start an activity, it pushes into the Activity Stack. So when Activity A calls Activity B, Activity B is now on top of Activity B, and when you press the back button, it also does a pop in the Activity Stack. So in concept, Activity B is gone. Pressing a Home Button is different from pressing back, it pauses the Activity, therefore it still eats a little of the phone's memory.
Here is a good explanation of how Android Manages Activities.

Android activity that I never want to start on

I have an activity that is themed as a dialog. I have seen that if the dialog is showing, and then I press the home button, and then using the task manager, restart the app, that dialog activity will be the activity that the app starts in, with no other activities available to go back to. That is, the activity that was running when I loaded the dialog activity is not running. So I just have this dialog-themed activity hovering over the desktop. That makes sense.
Looking over the Android activity lifecycle, the OS does remember the last activity and attempts to restart there. So I created all of the on* methods in my activity (onResume, onRestart, etc). What I found was really puzzling. When I restart the app from the task manager, the following methods are called:
onCreate()
onResume()
onStop()
onDestroy()
Where I was really just expecting
onRestart()
onCreate()
onResume()
Why are onStop and onDestroy getting called right away? And why does the dialog still show, even though onDestroy is called?
How can I configure this app so that it never starts solely on this dialog? I would be fine with the app restarting with the same "parent" activity and the dialog above it (that is, just as I left it), or with just the parent activity running and the dialog dismissed.
In this case, you should use a call to finish() in your Dialog code. You want to do this when the user transitions away from your app (which can happen when they go to the home button, they get a call, etc...). In this case, you would want to make a call to finish() in the onStop() of the Dialog. Calls to finish the current activity remove it from the stack, getting you essentially the behavior you describe.

Is there any way to distinguish between an Android Activity onResume from the home screen?

Is there any way to tell whether an Activity is being resumed (i.e. onResume is called) from the home screen/launcher?
For example, if I have an Application with two activities, A and B.
Scenario 1:
Some user action on Activity A will invoke Activity B, bringing it into the foreground - moving Activity A into the background. As Activity A moves into the background, it goes through onPause() and onStop(). The user (now on Activity B) either finishes the Activity or hits the "back" button, bringing Activity A back to the foreground, causing an onRestart(), onStart(), onResume() sequence.
Scenario 2:
If the user hits the "home" button while Activity A is in the foreground and then re-invokes the Application from the launcher, it goes through the same lifecycle as in Scenario 1. I.e. User hits the "home" button. Activity goes through onPause() and onStop(). User launches the application again, causing Activity A go come back into the foreground, again going through the same onRestart(), onStart(), onResume() sequence as in Scenario 1.
As far as I can tell, the Activity has no way of knowing how it was resumed, it just knows it is being brought back into view. In fact, I have a feeling that there isn't really as much of a concept of an "Application" in Android - in the sense of something that has a single entry and exit point.
in Scenario 2, your activity will get an onNewIntent call, with the launcher intent passed to it.
You could capture the back button press on Activity B and pass an extra value to Activity A. If there is an extra value then the activity was resumed from pressing back on Activity B, if there is no extra value then the Activity was resumed from being hidden.
Could Acitivity A use startActivityForResult() to start Activity B and use onActivityResult() to detect that Activity B finished?
So the straightforward answer to the initial question is probably: no. Launching an activity from the home screen through an icon, or resuming it from the recents screen can not be observed from the intent it is (re)started/resumed with.
Depending on what you are trying to achieve there are some approaches though:
what #superfell suggested:
Check for whether the onNewIntent-method is called on your activity to decide if it was restarted from the launcher. As a precondition you need to set your activity to singleTask/singleTop launchMode in your Manifest:
android:launchMode="singleTask"
depending on what you're trying to achieve, this might be enough! But additionally you might have to deal with what happens when the user presses the back button. Default behavior is to finish & destroy your activity. Thus a brand new copy of it would be launched when it gets selected from the recents screen. Though onNewIntent would not be called, everything would be rebuilt from scratch. If you need to prevent this you can use:
override fun onBackPressed() {
moveTaskToBack(true)
}
finally when you navigate "back" from an activity you launched yourself onNewIntent will also not be called. If you further need to distinguish why your activity is resumed, you might want to start the 2nd activity with startActivityForResult so the onActivityResult is called when you get resumed.
Launcher activity & Intent extra
A completely different approach would be to have a "launcher" activity in your manifest that directly calls your "main" activity and finishes itself. When calling your main activity you can put an intent extra that allows your main activity to distinguish if it was just launched for the first time, or not. As the extra would be present on further onResumes, make sure to overwrite it the first time you "consume" it:
override fun onResume() {
super.onResume()
val firstLaunch = intent.getBooleanExtra(FIRST_LAUNCH, false)
intent.putExtra(FIRST_LAUNCH, false)
if (firstLaunch) {
// do something
}
}
and when starting from your "launcher" activity:
intent.putExtra(FIRST_LAUNCH, true)
startActivity(intent)

Categories

Resources