How to prevent Android OS from closing the background application for memory?

I created an application that uses ~ 10 MB of RAM. It seems that when I launch other applications and my application is in the background, it sometimes closes. I suspect that this is due to the fact that the Android OS closes background applications for RAM management purposes (the phone has 1024 MB of shared memory).

Is there a way to keep the application always in the background programmatically or otherwise?

+4
source share
2 answers

Use the service to run in the background.

More details in Launch background service .

+3
source

. , , // ..

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

//saving
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

//restoring
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...

}

+3

All Articles