What I am trying to say here is the problem I'm facing to relaunch my app from same state where it was left before. I have did lot of R&D on this and have gone through the questions posted in stackoverflow.So please it is not necessary to say that it is a duplicate.
I tried putting these options
objIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
while starting the activity. Also I have tried this,
objIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
And finally I tried this method also
objIntent.setAction(Intent.ACTION_MAIN);
objIntent.addCategory(Intent.CATEGORY_LAUNCHER);
But none of these seem to be working for me.
The scenario goes like this, where this fails.
I run my application, go to some activity from the initial activity say from A to B
Now I will press my phone home button and do some actions like clicking photo and etc.
Again I try to launch the app from home screen.
Oops!! Now my app has not retained its state, it is launching from the initial state.
Note:
However this is not true in all the cases. some times say around 1 or 2 times out of 10 or 15 times, it launches from the same state where it was left before.
One more doubt i have is do I need to set this for all the intents? Say i have some 10+ positions of Intent i.e., starting the activity. Do I need to add this piece of code in all those places? I included this for 2 or 3 cases
Update:
This works in the debug mode, but when it comes to the point of an apk create, it is getting failed. However, if I try number of times it gives me the required result and there after behaves in the way as expected. Breaking my head to understand why it happening like this.
Any one has the answer for this?
If you want to "relaunch" your app in the same state that user left it in, you should save all your app state in the
onSaveInstanceState(Bundle)
method, and restore it inonRestoreInstanceState (Bundle savedInstanceState)
method. Like Saurabh mentioned in the previous answer, you have no control over when your app might be killed.You can use one of several ways to "save" your state - SharedPreferences and SQLite DB being the prominent ones.
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.
Yes, finally it is possible!! Thanks to google forum. I referred this https://github.com/cleverua/android_startup_activity.
What I did according to this is, create a startup activity. Inside it's oncreate method, check for a custom method called needStartApp() or something with your convention. In this get the running tasks info and compare it with our application package name. And if it is true, start your initial activity. Rest everything goes fine.
Here is the extra activity you should add,
At the end, do not forget to add this permission to the manifest
"Android OS is really good, however it is not perfect."