I am trying to understand some behavior of the android apps, now that I faced the following issue :
After device restart my app launches by default, but with one issue : If I put it in the background, and I try to resume it from HOME launcher, it restarts from splash
Weird is that this happens ONLY & ONLY after the device it was installed on, is restarted ! To break this behavior I have to kill the app and launch it again. From that point it works as expected -> If in background, using launcher icon restore the app state
Looking a bit in the logs from lifecycles of the activities in cause, I noticed that the flow is pretty much the same ! With the difference that after onResume of last active activity, the SplashScreen gets launched (this being LAUNCHER activity)
Now, I've seen that this might be already an issue reported to Google, even multiple times, but .. If I cannot fix or workaround it, I am at least trying to understand the flow of what happens on these cases.
Edit:
Seems to be related to FLAG_ACTIVITY_NEW_TASK set in the Receiver, but.. then again I cannot start the app after boot is completed without this flag! Feels like a cycle I can't get out of..
I do a research about Android Activity lifecycle. And I know after an app goes to background, it is likely to be killed by system if apps with higher priority need memory, but save the activity with a bundle. And when users come back, system will restore it, that makes user feel good!
I want to know how to make this circumstance, I want to see my application to be killed before hand, not see it when it is used by users, to evaluate whether it is strong or not. Because it may be crashed when this happens.
Enable "Don't keep activities" in the developer options on your phone. This will force kill your app as soon as you put it in the background.
See this question for more detail about how it works:
Whats the main advantage and disadvantage of "do not keep activities" in android
See here for more info on how to enable it:
http://www.pcadvisor.co.uk/how-to/google-android/3590299/32-useful-things-you-can-do-in-android-developer-options/
You can emulate it by sending your app to background and killing it by adb shell am kill your.package.name.
I thought about one android application,so is there possible that user can not kill my android application manually or android os can not kill my application's process until user uninstalled it?
Do you know how to do that?
If anyone knows than please tell me.
No that isn't possible. User always can kill your app and OS also always can kill your app. One you can do is use onBackPressed() method to do something on click back button by user.
EDIT : You can also create a background service, which will relaunch your app on kill, but remember that service is also "killable".
You can create a background service that will relaunch your application once it is killed by the OS.
Just don't forget to save your data.
P.S. This is not how a good application should behave.
I have written a simple database program in android. It runs fine, there is no force close error. But I checked from my application from Settings App I see the Force Close option enabled, which implies that my application is still running in the background, even though I have completely came out from my application to the home screen by pressing back key. And moreover I am not using any services, alarm or broadcast things.
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Can some one please guide me what may be the probable reason?. Or is it okay? Or will it crash if I put it on device?
Your application is alive until Android OS needs more memory and destroys it. What I have understood does Android start destroying activities before killing the whole application. This means that your application can be alive even if you have finished your activities.
Do not worry about this; Android OS is handling this extremely well.
Android doc say:
"When the system, rather than the user, shuts down an activity to conserve memory, ... "
But how to simulate this situation?I want to debug the onRestoreInstanceState(Bundle) method,but don't know how to.
You can't do it in an automated way b/c its completely non deterministic.
See my answer here: https://stackoverflow.com/a/15048112/909956 for details.
But good news is that all you need to do is just simulate calling onSaveInstanceState and you are indirectly testing this low memory situation.
onSaveInstanceState can be triggered by:
losing focus (by pressing home which in essence is like switching from your app to launcher app), launching another activity, pressing recents
changing orientation. this is the easier way if you are using an emulator
changing developer setting: goto developer options --> Apps --> Don't keep activities. This is best option if you are testing temporarily on an actual device.
I've used the "Don't keep activities" developer option to reproduce a crash that happened when an activity was killed due to memory pressure. You can find it in the Apps section of Settings->Developer Options.
It destroys every activity as soon as you leave it. E.g. if you press home to put your app in the background the current activity is destroyed. See https://stackoverflow.com/a/22402360/2833126 for more information.
There's two way to simulate the android killing process: using the setting "Don't keep activities" in developer settings or killing the app process by yourself.
To kill the process, open the activity you want to test, then press home button to send your app to background, and then, using the DDMS in Android Studio (Android Device Monitor), select the process and then stop the process (as seen in the image below). Your app was killed. Now, open your app again (accessing the list of open apps). Now you can test the killed state.
For the purposes of debugging onRestoreInstanceState(), just change the screen orientation ([Ctrl]-[F11] in the emulator). Your activity will be destroyed and recreated, and the onSaveInstanceState()/onRestoreInstanceState() pair will be invoked.
Use the SetAlwaysFinish app (works on a real device and in the emulator) or use the Google DevTools app (works in the emulator only).
These apps use the hidden AlwaysFinish setting of the ActivityManagerNative class to change the behavior of the OS and cause it to immediate unload every activity as soon as it's no longer in the foreground. This will reliably trigger the onSaveInstanceState and onRestoreInstanceState events.
See link below for more details:
http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/
To debug onRestoreInstanceState you could do the following:
make sure you can debug application right after its start (calling android.os.Debug.waitForDebugger() from your constructor helps, it hangs your application until debugger is connected),
put you application in some state,
causally kill it from Settings->Apps,
causally switch back to it through Recent Apps button (it will still be in the list),
at this moment your application will be started anew and onRestoreInstanceState will be immediately called on the top activity.
Good answers here.
Now, residing in the distant future, using Instant Run in Android Studio will also trigger a save and restore when activities are restarted with code changes.
There's a decent solution for this in Android 6 and newer. See my answer here: Simulate killing of activity in emulator