What am I missing here?
Problem is, I launch my app onCreate is called. I press the home button to leave it, press the app icon again and onCreate is called yet again. The activity is being killed, right? Well, if I press home to leave the app, hold down home, then pick the app from the running apps it resumes where I left off. So hitting home within the app is not killing the activity. Press the button to open the app is killing the activity.
I use Eclipse. I just downloaded the latest Eclipse and re-downloaded ADT and updated the Android SDK. Everything is up-to-date. I even uninstalled all java and downloaded and re-installed the latest jdk and runtime on my computer. Then re-signed the app.
Using Eclipse I added the skeleton sample project. Signed in debug mode and it resumes every single time, just like my real app. Signed for release and it has the problem: press home to leave the app, press the icon to launch the app and onCreate is called again. Literally every single time. Shouldn't it be resuming?
The problem is it's not just me (I have a BIONIC). It happens on other peoples phones and it happens on the emulator, but it only happens when signed for release. Signed for debug and it's perfectly fine.
Any ideas on this?
According to this previous question you need to set the FLAG_ACTIVITY_REORDER_TO_FRONT flag using setFlags(). This would cause your running activity to be reused instead of recreated every time an application starts it. When you press the icon on the home button, the activity is started. The home screen doesn't know if the application is running or not, so it just starts it. When you long press home, it actually switches to the activity as it knows it's running.
Another option seems to be setting android:launchMode to singleTop.
Related
I am working on a fitness app which has a home activity which launches a workouts activity which launches a specific workout activity. In the workout activity, one may start a workout. Thereafter, one might want to then press the Home button and launch a music player or perhaps the web browser. At some point, one would probably launch the app again to return to the already running workout, but that ends up launching a new instance of the app. When I set the launchMode on the home activity to singleTask, it simply goes back to the existing home activity when I tap the launcher icon. What I would like is for it to go back to the workout in progress, which is where you would depart the app.
Essentially, I'm looking for behavior identical to iOS where it would simply restore the app to its current state if you "relaunched" the app and it was still running.
It is supposed to work as you've described. In most cases, it actually does work like that. However, there is a long-standing nasty Android bug which causes the behaviour you've described. This happens when you launch the app for the first time from an IDE (like Eclipse) or by clicking the "open" button on the Installer screen. To see if this is what you're seeing, just do this:
Go to Settings->Applications, choose your app and click "Force close"
Launch your app, do something, press the HOME button
Launch your app again.
You should return to where you left off. If not, something else bad is going on. If that is the case, please post your manifest in your question, because the problem is likely in these.
Don't try to use special launchModes to fix this. This just creates more problems.
See this answer for more information about the nasty long-standing Android bug.
1.install an apk from app installer
2.then just click "OPEN" to launch it at once
3.after the app launched and then press HOME key
4.find the app from app list and click its icon to launch again
5.then the app will be launched with a new instance.
And if you repeat 3~5 several times, it will repeat create a new instance. if you press "BACK" key now, you will see the app is still there for the same times you launched.
But if you just click "DONE" at step 2 and then launch the app from app list, everything will be OK then.
Why?
The app installer (as well as many Android IDEs) use different intent flags than the regular app launcher does, which means that the launcher's intent doesn't properly match with the Activity's existing intent and it ends up creating a new activity on top of the stack.
I think this question is similar to what you're asking about:
Activity stack ordering problem when launching application from Android app installer and from Home screen
hi guys i am finding the strange behaviour in android while launching the Application. Let me explain the senerio. I am launching my application from android's launcher page and my application starts and runs fine and after few minute i press home button and go to android home page and then go to launcher page and again select my application and it is starting it again from first but it should have resumed from the last place where i left. And when i press back button on the launch screen of second instance of my app i am able to go back to the last page where i have left. I am more confused about what was happening and it too happens sometimes only not every time. Hope you people could help me sort this problem, Hoping for better responses. Thanks in Advance.
Edit #1:
It is not happening in all the device it happens only with Samsung and Sony but works fine with LG and HTC.
To keep an activity running in the background is not in your hand. When you press the home button, your current activity goes to the background and can be killed (onDestroy() will be called) at any time depending on the need for memory of the other applications you launch.
The more apps you launch, the more chances of killing your background app is.
The behavior may be device specific - try saving your game settings in a persisted location within the 'onPause()' function, and retrieving it on 'onResume()'. Then it doesn't matter if a new activity gets launched or the old one gets called.
I think I've gotten myself seriously confused. Up until recently my app was working great. I hadn't changed anything except updated java and android tools. I'm not sure if that caused my problem or what, that's all I can remember changing related to it.
Here's the problem I'm running into, which just started.
Debug mode (installed app on phone through Eclipse):
Tap app icon to open app, onCreate is called
Hit home button, state is saved
Tap app icon to open app, it resumes, onCreate is not called
That worked, my app is happy.
Now I export the signed and zip aligned app, remove the app on my phone, install the new one, and repeat these steps:
Tap app icon to open app, onCreate is called
Hit home button, state is saved
Tap app icon to open app, onCreate is called again
Ugg, onCreate is called again? Bundle is null, my variables reset. This happens on the emulator too and for other users of the app. I can hold the home button down and switch to it and it continue where it left off but if I tap the icon it calls onCreate every single time.
Why is this happening? My app is a single activity.
My issue is like this, I press home button and go to home page, then I press the app icon to get back to the app. Instead of resuming the app it starts from the beginning. But if I select app from recent app list, it resumes. So, some unknown reason app starts from the beginning if I press app icon.
I'm getting this strange result with signed apk. Unsigned/debug apk works fine, it resumes.
Please note that, I've not handled any BackStack activities, neither handled any activity android:launchMode.
At last I'm answering my own question. The issue is solved.
It turned out that Activity Standard LaunchMod (which is default settings) behaves like this in Some devices with OS v5.0 +. So, I added it to be "launchMode" in from menifest. So App works fine now. Although I'm still not sure about this behavior.