My android app is getting restarted after force close, through my entire application which consist of 20 activities, I am relying on static data created on a main activity. So once the app is getting crashed all my static data is getting lost and when the app auto restarts practically it does not have any essential data to operate upon.
My question is, upon crash i want this things to happen
- If the app crashes, I don't want the app to restart rather I want all the stack/task related with this app to be wiped out of memory. A user can restart it from the beginning again
- If I can't prevent app from restart, at least I want to preserve essential data so that when the app restarts I can assign them back. Also when it restarts I want my app to start from the main activity.
I know when activity crashes android system will bring next activity in stack to foreground, and this is reason for my app producing redundant results. also i went through the android developers but only thing i got to know was setting up an attribute in Manifest android:finishOnTaskLaunch="true"
. But unfortunately this is of no help to me. I would appreciate your help on solving this issue, and also letting me know the cause and analysis.
My app was also getting resumed with blank screen, when it was getting crashed. To resolve this, I checked the savedInstanceState object on onCreate method of my main activity and if it is not null (means it is restarted by android system) then I finished my activity. Something like that:
It may help in your case too.
(reference: Archie.bpgc)
Do not store data in static fields. Your process could be stopped on low memory event and you will lost everything. Your activities will be restored from saved state if user switch to your app again, but your static variables will not get restored.
Well, an application is not only interface(activities). Imagine you have some complex enterprise application, using SQL transactions, security, Web Authentication, etc.. It is almost impossible to make each activity able to recover the whole app context using only Shared Preferences. So in this case, I use this piece of Code:
Best solution would be instead of using Static data, use
Shared Preferences
or store data in aDatabase
and if anyuncaught Exception
occurs, show a message likeApplication has crashed and a report is sent to the admin
and then restart the Activity that caused the Crash. This way user can continue using the application.Do the same but instead of restarting the Activity which caused the Exception restart the application.
create a class used to handle
unCaughtException
and in every Activity create an Object of this class and set it as the
DefaultUncaughtExceptionHandler