I have an app that crashes after the RAM memory is cleared. I cannot use the onSavedInstanceState
because of the current implementation. So, does anybody know, how could I just restart the app when the user tries to open it from Recent Apps? I already tried this code in the Main activity which is the base class for all activities:
if (isFirstApplicationStartUp()) {
Intent i = new Intent(this, Main.class);
startActivity(i);
}
isFirstApplicationStartUp() is a boolean set to true from a class which extends Application (in onCreate).
But this implementation does not work as desired as the previous activities get to be called before this code is executed.
use shared variables instead of global variables in the activities. This solved my problem.
The android OS is responsible for clearing up ram when it needs it. This means it can liberally kill any app when it is not in use. The app itself tries to save the view components of the app but you are responsible for saving any other variables or restoring any images.
Here is a more detailed explanation: http://www.senzumbrellas.com/collection/home.php?sl=en
Instead of setting this boolean, Android gives you a way to access any instance information. You should instead override onSaveInstanceState(Bundle savedInstanceState):
Then in onCreate check savedInstanceState in onCreate:
You probably don't want to have the app restart from the beginning when being launched from the "recent tasks" list, because your app may be perfectly capable of working. What you need to do is you need to remember if your app has been properly "initialized" (whatever that means). If the user returns to your app and the process has been killed and restarted since your app was initialized, you need to detect this condition and then redirect the user back to the first activity of your app.
The best way to do this is to have a base class for all of your activities. In this base class you implement code in
onCreate()
that checks if your app has been properly initialized or not. If it has not been properly initialized, you should redirect the user back to the first activity. Something like this:All of your activities need to inherit from this base class and they should NOT override
onCreate()
. Instead, they should implement the methoddoOnCreate()
, which will be called from the base class'onCreate()
(see above).NOTE: This only works if the root activity (in this example
FirstActivity
) is never finished until the app quits. This means that you will always have an instance ofFirstActivity
at the root of your task. This is required so thatIntent.FLAG_ACTIVITY_CLEAR_TOP
works correctly.