Android app crashes while clearing RAM

I have an application that crashes after clearing RAM memory. I cannot use onSavedInstanceState due to the current implementation. So, does anyone know how I could just restart the application when a 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 the boolean true value from the class that extends Application (in onCreate).

But this implementation does not work as desired, as previous actions are called before this code is executed.

I would really appreciate if anyone would help me solve this. Thanks in advance.

+1
android application-restart
source share
3 answers

You probably don’t want to restart the application from the very beginning when starting from the “recent tasks” list, because your application may work fine. What you need to do is to remember whether your application was correctly “initialized” (whatever that means. If the user returns to your application and the process has been killed and restarted since the initialization of your application, you need to detect this and then redirect the user back to the first activity of your application.

The best way to do this is to create a base class for all your activities. In this base class, you implement code in onCreate() , which checks whether your application is initialized correctly or not. If it has not been correctly initialized, you must redirect the user to the first action. Something like that:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check if the application has been restarted by AndroidOS after it has killed the process due to inactivity // in this case, we need to redirect to the first activity and dump all other activities in the task stack // to make sure that the application is properly initialized if (!isApplicationInitialized() && !(this instanceof FirstActivity)) { Intent firstIntent = new Intent(this, FirstActivity.class); firstIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // So all other activities will be dumped startActivity(firstIntent); // We are done, so finish this activity and get out now finish(); return; } // Now call the activity-specific onCreate() doOnCreate(savedInstanceState); 

All your actions should inherit from this base class, and they should NOT override onCreate() . Instead, they should implement the doOnCreate() method, which will be called from the onCreate() base class (see above).

NOTE. This only works if the root activity (in this FirstActivity example) never terminates until the application shuts down. This means that you will always have an instance of FirstActivity at the root of your task. This is necessary for Intent.FLAG_ACTIVITY_CLEAR_TOP work correctly.

+2
source share

The Android developer is responsible for cleaning up when necessary. This means that it can easily kill any application when not in use. The application itself is trying to save the components of the application, 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

By default, the system uses the state of the Bundle instance to store information about each View object in the activity layout (for example, a text value entered in an EditText object). So, if your instance of activity is destroyed and recreated, the layout state is restored to its previous state without any code required by you. However, your activity may have more status information that you want to restore, such as member variables that track the user's progress in activity.


Instead of setting this boolean, Android gives you access to any instance information. Instead, you should override onSaveInstanceState (Bundle savedInstanceState):

 @Override public void onSaveInstanceState(Bundle savedInstanceState) { //save any state variables you want to keep around. //this is not rally meant for large memory intensive objects like images super.onSaveInstanceState(savedInstanceState); } 

Then in onCreate check savedInstanceState in onCreate:

 if(savedInstanceState != null) { //this means that the activity is being restored } 
0
source share

use shared variables instead of global variables in actions. This solved my problem.

0
source share

All Articles