I have an issue where I want to differentiate between an activity being stopped because the user wants to navigate back (staying active within my app), or putting my app into the background (switching to the home screen or another app).
Is there any way to tell during onPause or onStop which of the two is the case?
Related
I am starting a service in my app. On click of service I am launching an activity. The service click event works fine when we go to any app and press back button and exit the app.
But if we are in any app and then press home button and click on the service the activity is not launched. If I click it more then 2 times,it opens the activity and sometimes it opens the app also.
So i am unable to understand the difference between pressing back button and home button.
After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. The system retains the state of the activity - i.e. onSaveInstanceState will be called. If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack.
However, if BACK key is pressed, the current activity is popped from the stack and destroyed. The assmuption is the activity is done and will not be used again. So the system does not retain the activity's state - i.e. onSaveInstanceState will not be called.
Home Task :
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a Windows PC.
Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.
The Back button is the way to close apps so that they are actually closed.
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
In case of activities their default implementation is LIFO based in stack and works like:
On Back button Pressed: finish the current activity by calling stop method.
On Home button pressed: activity is being paused and then it may either resume if come back to it, otherwise system will call stop() method of activity to save unused resources and utilize memory.
but these functions can be edited by overriding if required.
What I do know is that the current activity get into the onPause() mode, and the home screen activity brought to front.
My confusion starts with situation you can re-open the application from the recent tasks menu.
so what exactly happening when I'm opening the application from the recent tasks manager?
Is the activity that was foreground when the home button pressed is still somewhere in the stack?
Is there more then one activity stack on the same time?
As far as I understand it, there is no real stack (of applications). Is just that your activity has states, so whenever you are pressing the HOME button your activity in your current application just "pauses" like if it was a stand-by state so multiple applications can be in this state as long as the system doesnt require memory and kill the tasks.
So whenever you open the activity from the recent tasks its just telling the application to wake up.
First of all, here is everything you need to know about the concept of the "Up Button":
Navigation with Back and Up and some of the implementation details: Providing Ancestral and Temporal Navigation.
Generally speaking, the Up button lets you navigate up in the application hierarchy, instead of just navigating back in the application(s) back-stack.
For example, if you work with some kind of app and you get the email notification, you can open the mail client by pressing the notification. Then you can go back to you application you were working with by pressing the Back button ( back-stack ) or you can press the Up button in order to go to the mail client's 'parent' activity ( for example from some EmailMessageActivity to EmailHomeActivity ) to work with the mail client application instead of the initial application ( the back-stack usually is cleared then, so you can only go back with the Back button as far as the the Android Home screen ).
The "Recent Tasks" factor is irrelevant and misleading, it's just another way of starting a new Activity.
Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.
Using my mom's phone with my app if i hit the home button and then navigate back to the app, it is calling onCreate again. If I understand correctly this should not be the case right?
I know this is happening because my webview loads and it should just open up the page that was already open. Especially since I have the launch mode set to singleInstance...
What are some reasons to why it would be called twice on my mom's phone, but on my phone it works exactly how it should?
According to the Android documentation, your Activity can be killed at any time when it's not active which is the case when you switch back to the home screen.
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.
My concern is, how long will our activity be there in the stack?
What I mean is, when the home button is pressed from my app and then my app is launched again by clicking its icon, it will regain the correct place from where I pressed the home button without writing any special code for this.
I don't know whether I'm right, I think that my app is pushed to a stack, and when I launched it, it's pulled from the server.
I want to know how long will be in that stack or any other place where it is stored? Will it be replaced by some other app after pressing the home button and using many other apps?
Your activity will remain loaded until Android decides it needs the resources back. There are methods you can override in your activity to deal with this. Read more here.
Edit: See comment below for newer link.
You should also see the documentation on the Android Activity Lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
and Process Lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
http://www.edu4java.com/android_course/processes-and-threads.html#Lifecycle
There is a hierarchy of which activities will be killed first when Android needs to reclaim resources:
empty processes (hosting no activities or other application components)
background activities
service process (running a service started with startService())
visible activities (visible on screen but not in focus, such as one behind a dialog)
foreground activity