Unable to understand meaning of 'Partially visible' - android

I am trying to understand android activity life cycle.
Official documentation states:
Pause Your Activity:
When the system calls onPause() for your activity, it technically means your activity is still partially visible.
I created an activity and then moved to another activity using intent.During debugging I see that first activity's onPause() is indeed called.But I don't understand what does it mean for activity to be partially visible because when other activity is visible(and first activity is paused) then first activity is not visible at all.
With regards
Manish

to be partially visible is like for situation like poping a dialog themed Activity up over your Activity means that the activity is visible for user even it lost interaction with user. onPause() will run in Activity Transitions and after that when its no longer visible by user , onStop() will call.

Related

How Onstop Method work for this demo?

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 :)

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

Android back button behaviour

Let's say we have a default, empty activity with default behaviour, launched with default intent flags. User presses back button on the device. The activity disappear... but how, actually?
Is pressing back button behaving the same way like finish()?
Is the activity immedietely destroyed (onDestroy is called)?
Is the activity guaranteed to be destroyed, but not immedietely?
Is there any chance the activity won't be destroyed and this instance will be reused when this activity is launched in the future? (so only onPause and onStop -> onStart and onResume are called?)
I'm looking for a reliable answer, so please do not answer if you are not absolutely sure what happens here.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
This is a subchapter from the official Android documentation that addresses your question. It is a subchapter of the topic Managing the Activity Lifecycle, which can be read here:
http://developer.android.com/training/basics/activity-lifecycle/index.html
It is definitely worth reading the whole chapter to get to know the details about Androids Activity behaviour. But the subchapter ( first link ) is the relevant part to this question.
you use should look into this try this
and please tell specific what you wish to do with back button for your default activities ......
When you press back, (if not intercepted by anything like the keyboard, fragment, activity, etc) the OS (via ActivityManager probably) will try to show the user the previous activity in your current task (again, ignoring fragments' back stack).
If there is no such activity, the task will be terminated and you'll go to the previous task - the home screen most of the times or some other application that might have launched your app.
You'll get onDestroy called soon (it depends on how long it takes to start the next activity but on a good phone it should be under 100-200ms).
Your activity instance won't be reused after onFinish. This happens before the activity is destroyed so if you need another activity of the same type, the OS will create another instance.
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is guaranteed to be destroyed, but not immediately, may be when the system resources are low) and the previous activity resumes (the previous state of its UI is restored).
Which actions does the back button/back key on Android trigger?
Definitly onDestroy() is called .....There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

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.

Which actions does the back button/back key on Android trigger?

I am really confused. I have read that the back button
calls onDestroy()
can close up your currently-running activity
calls onPause()
I think onPause() should be right. But this is an side effect, because the Activity gets into the background. I found nothing in the docs. But maybe I have overlooked something.
Can someone please explain to me what the back button is supposed to do programmatically? Some references would also be nice. :-)
I have read that the back button calls onDestroy(), can close up your currently-running activity, calls onPause()
All three are correct.
I found nothing in the docs.
Quoting the Android documentation:
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
To elaborate, if there is nothing else that will consume the BACK button press (e.g., an open options menu), your Activity will be called with onBackPressed(). The default implementation of this calls finish(). This will take your activity from the running to the destroyed states, calling onPause(), onStop(), and onDestroy() in sequence, as shown in the event flow diagram:
Just to add, browser application overrides onBackPressed() to go back to previously opened tabs (if available) and it not, closes the application.

Categories

Resources