In iOS if user press home button, app will move to suspended state. Double click of the home button, suspended apps appear, and selecting an app will bring back it to active state.
How is it by Android? App can become to suspended state pressing center button? Or app move to not running state?
If suspended state exist, how to bring back to running state, where I can select one app among many?
In an activity the oncreate method will get called only once, when app first starts? Which method is get called that will called always when app appears, come back to running state?
Have a look over Android Activity Lifecycle
In android, if the app is not "on the main screen" it counts as Paused state.
By starting an app, there will be a one and only call for onCreate, later will be called onStart and onResume. If you will now click the home button, the onPause and onStop method will be called. Once you wish to back to the activity you can press the Overview button, and select your app from the list and the method onRestart, onStart and onResume will then be called.
If your app is not fully hidden, e.g you swing your finger from top to bottom to see "notification center", this will cause skipping the onStart, onStop and onRestart functions.
Related
I wanted to understand when an activity can be in the paused state, so onPause is called, without going into the stopped state (onStop) after. Pretty much like onPause() -> onResume() without passing by onStop -> onRestart -> onStart
onResume method is called when the activity is in focus meaning the user can interact with the visible activity on the screen. onPause is called when the activity loses focus i.e. the user cannot interact with the activity but the activity is still partially visible to the user. onPause and onResume can be seen in action when interacting with a dialog box.
onResume to onPause
When a dialog box appears in front of the ongoing activity, the activity loses focus because the dialog has to gain the user's focus. The activity lifecycle goes from onResume to onPause state. When the user has responded to the dialog and the dialog dismisses then the activity again gains its focus. This time the activity lifecycle goes from onPause to onResume.
onStop is called after onPause only when the android OS detects that the ongoing activity has a chance to get destroyed. This usually happens when activity goes in the background (in recent apps stack). Android system automatically destroys apps from the recent apps stack to free up memory space if those apps are not opened by the user for a while.
On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.
I want to clear shared preference when I press home button then the app is cleared swipe or simply cleared from device ram. What callback should I use ? (Like OnDestroy, but OnDestroy is not working for me)
Use onPause() for doing stuff when the activity loses focus (multi-window mode, home button pressed, swiped away, or anything that takes focus away from the activity). The rest aren't guaranteed to be called.
I have a home screen widget with a button with a pendingIntent. Click it, it bundles a boolean to indicate where it came from and it brings up my main activity.
In my main activity in onCreate, I check for a bundle, if the boolean that says it was launched from my widget is true, I perform an action.
Every time I hit my widget button, it should launch my main activity, and run through onCreate.
And it does, on the emulator. And it did, on my n1. Then suddenly it stopped on my n1. I can still get it to hit onCreate when I launch from the widget but I have force stop or reinstall and then it only does it the first time.
What is going on with this?
What's going on is the Activity lifecycle. Android will keep the same instance of your Activity around so that the user can return to it later. Monitor some of the other lifecycle methods like onStart and onResume to see this in action.
You may see a difference in behavior if the user hits the back button vs. the home button. The back button will finish the current Activity by default, whereas home will leave it alone unless Android decides to kill it for resources later.
Let's say I have a Hello World single Activity application. I start this application.
What methods are invoked in each case:
Home button is pressed: ?
Back button is pressed: ?
Phone call is received: ?
What methods are invoked once the user starts the application again via the app icon (assuming the OS hasn't had a "other apps need memory condition"):
Home button was pressed: ?
Back button was pressed: ?
Phone call was received: ?
Thanks all.
Edit: Extra Credit: How can the user invoke onPause without invoking onStop?
both pressing home button and receiving a call don't remove the activity from the task's stack, and will be available when you re-enter the app => onPause() => onStop().
as the activity lifecycle diagram shows, re-entering the app calls => onRestart() => onStart() => onResume()
pressing the back button instead kills the activity => onPause() => onStop() => onDestroy()
re-entering the app in this case calls the classics => onCreate() => onStart() => onResume()
EDIT
from http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If an activity has lost focus but is
still visible (that is, a new
non-full-sized or transparent activity
has focus on top of your activity), it
is paused. A paused activity is
completely alive (it maintains all
state and member information and
remains attached to the window
manager), but can be killed by the
system in extreme low memory
situations.
There can be several scenarios
Opening the app from the app icon. following methods are called
onCreate()-->onStart()-->onResume()
When user presses the home button
onPause()-->onStop()
When user returns to the app from the Activity Stack
onRestart()-->onStart()--> onResume()
When the app is running and user presses the power button
onPause()-->onStop()
When user unlocks the phone
onRestart()-->onStart()--> onResume()
When user gets an incoming call while you are in the app
onPause()
When user returns to the app after disconnecting the phone call
onResume()
When user presses the back button from the app
onPause()-->onStop()-->onDestroy()
And when the user presses the home button and from the activity stack user swipes the app.onDestroy() method may or may not be called depending upon the OS contains the context of the Activity or not according to memory requirements
Well see, while a sequence of events may occur with your hello world program, the same sequence may not occur in say a video game, because Android will probably Destroy it for taking up too much resources.
The best way I have found to see the lifecycle for my app is to override all the methods (onStart, onRestart,..., including the onSaveInstance and onRestoreInstance) and insert log statements in each one. Like so:
#Override
public void onDestroy() {
// Call the super class
super.onDestroy();
// Log the action
Log.d("Debug", "onDestroy() has been called!");
}
Now I can go to logcat and see what events took place.