In most Android apps, when you press the Home button to "minimize" the application and then open the application again, you will be taken to the screen you were last on in that Application.
I've read the following:
When launched via icon on the home
screen, Android will always start the
activity with the
android.intent.action.MAIN filter in
your AndroidManifest.xml, unless the
application is already running (in
which case it will obviously restore
the activity on top of the stack).
but this is not happening. When I launch the application a second time, it takes me to the main Activity. The application isn't being terminated. If I then navigate to the screen I was last on, all the data and such is there.
How do I make it do what I want? The related questions all seem to pose the same answer (the one I quoted above), but this is not holding true.
Are your activities in the same task? That is, are you starting any of your activities as FLAG_ACTIVITY_NEW_TASK? I'm not positive, but that could change on the resume behavior works.
Related
I have implemented a splash screen for my app. I made the splash screen activity the launch activity. The splash activity does the startup work (loading data, etc.), and then launches the "real" main activity.
The problem is this: I am using a 3rd party app that has the ability to launch other apps. Let's say my app is already running (it is past the splash screen, and is on to the real main screen). I then press the Home button to put the app in the background. I then do something that causes the 3rd party app to launch my app. What I want is for my app to be simply restored (as it would if I had tapped the app icon from the phone's regular launcher). Instead, it launches the splash screen all over again, and my initialization code runs again, which I don't want.
Since this is a 3rd party app that is starting my app, I don't have the ability to change how it launches my app. I am assuming that it is just querying the package manager to get the launch intent and starting that. So, I need to find a way to fix this in my app itself.
Is there a way that I can find out from my splash screen's onCreate method if my "real" main activity is already running, and if so, skip the initialization code and bring the existing main activity to the foreground?
Keep a static boolean in your Application class.
When initialization is done, set it to true.
When splash screen starts, check the value of the boolean, if true, go straight to your main activity without doing any logic, and instantly finish the splash activity.
If false, then assume it's a cold start and you need to run the initialization code.
There is no way to look at the Activity stack from within an Android app, so you can't check if the Main activity is already running.
There is also no way for the 3rd party app to check if your app is running, and then launch a different activity depending on that.
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.
When I open my app for the first time, it launches the splash screen, then goes to a home page.
Usually when I reopen the app (by pressing the app icon) I am directed to the home page properly. However, sometimes I get the splash screen again.
This wouldn't be a problem (because it means I'm reopening the app) but when I press the back button from the home page, I see the last iteration of my app (still open and running).
So what is going on?
Thank you
Try adding launchMode="singleInstance" to your android <activity> manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Generally the default behavior of android apps makes sense. If you have multiple activities you generally expect that pressing the back button will move you back to the previous state. It is possible to have multiple versions of the same activity in your activity stack and negating this would not be expected. If I filled out a form twice then pressed back to the first form I wouldn't expect it to contain my second form's data, for instance. But for specific types of applications this does not make sense. There are multiple ways to handle this so you need to be aware of what you app will be doing and what the expectations for your app will be.
Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.
I have a pretty standard iPhone app that creates a series of around 7 unique Activities initialised by Intents.
However if the app crashes on the 7th Activity, the app restarts on the users phone around the 5th activity. The problem then is the info gathered from activities 1-4 is null, meaning the app is useless and the only way to get the app working again is to either continually press back or else kill the process.
Why does this behaviour occur, and is there a way to force the app to start back at the first activity when it crashes.
Your app is restarting in the activity that was beyond the crashed activity on the activity stack. You can finish all activities that are beyond your current one through calling
this.finish();
after starting the next activity.
The problem is that the user now can not press the back button to change data that was inserted in the steps before because those activities are gone.
You may have a general problem with data persistence over pause and resume cycles. Try to call your emulator or phone while you are in one of the deeper activities and then return to the app through long pressing the home button. You may see that the data from previous activities is now also empty.
Play around with this behaviour and have a look at the application life cycle documents.
This may be a way to check if data is available and if not close the activity or go back to the start activity.