Application context killed but activity not

I am working on an application in which there are several actions. One activity begins the next. To share some values, I use a custom application implementation (I'm talking about android.app.Application) called MyApplication.
As we all know, the Android system kills the application if it needs more space. But this leads to a problem:

  • I open the application and use it just like any other application.
  • I close it (Main button) and use other applications.
  • The system will kill my application because it will lose memory
  • When I open my application again, he wants to open the last action that I used, and my power is closed, because the values ​​in MyApplication are null

It is strange that the system destroys my application, but it looks like it remains active. I really don’t understand why this is happening because the application is not like the life cycle.

What I want to have:
When MyApplication (the whole application, not just the activity) is killed, I want the last actions to be killed. Therefore, when I open the application again, it starts the main work provided by manifest.xml.
or
Values ​​in MyApplication are saved and not lost if the application is destroyed. (I'm talking about a few objects, so I think that the general settings will not work).
I don’t want to use the service to bind my actions, but is there a way to tell the system that my last activity used depends on the application context?

I hope you understand what my problem is, and someone can help me with this.

+5
source share
3 answers

The right way to do this is to keep the application state. Reinstall the onSaveInstanceState (Bundle savedInstanceState) method to save your state and onRestoreInstanceState to get it.

If you need to save large amounts of data, consider using an SQL database

+1
source

, , , onPause(), onResume() onStop(). savedInstanceState, ( , onCreate)

0

In your custom application implementation, add the flag:

public boolean appContextExist = false;

In the first step, set the flag to true,

Override the onCreate and onResume methods in your activity that needs contexts, add the following:

MyApplication myApp = ((MyApplication) getApplicationContext());
if (!myApp.appContextExist) {
    // Code to return to start activity here
}
0
source

All Articles