Android app is being killed when it keeps in background for long time

1.2k Views Asked by At

My android app is being killed by the Android OS when it's keep in background for sometime.So when it launches back it's an empty screen since all the data has been wiped out at the time when OS killed the process.Unfortunately I'm not storing any data in SQLite or sharedpreference. What would be the best way to show the UI component with data even after app being killed?(Unfortunately couldn't implement SQLite due to the sensitive data/as per requirements).

1, I'hve observed whenever this happens in the base activity oncreate method I'm receiving the savedInstanceState in oncreate method. So I'm just calling the launcher method from there and app is working as expected. But is this the perfect way of implementation?.

2

There are 2 best solutions below

3
On

most likely the server-side session had expired in the meanwhile.

  • logout the user on onPause() and login again on onResume().
  • or use a service to keep the server-side session alive.
0
On

If I understood correctly, you should be quite right with savedInstanceState, regarding saving simple state and restoring from it.

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


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's 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);
}

Then when the app is killed and restarted:

@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
    }
    // ...
}

Just refer to here for more details.