0

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?.

Manu Ram V
  • 369
  • 1
  • 3
  • 23

2 Answers2

1

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.

Wei WANG
  • 1,748
  • 19
  • 23
0

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.
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • No.The access token is valid even after killing the app. Server side has no role in this. – Manu Ram V Dec 21 '18 at 04:03
  • this issue is still not reproducible... especially when not even "long time" has a value. try to check for `if(savedInstanceState == null) {}` and only manipulate the view then. – Martin Zeitler Dec 21 '18 at 04:05
  • It's reproducible as I said earlier Android OS is killing the process and which is very randomly. If the App is not killed then no problems user can be presented the UI with data. – Manu Ram V Dec 21 '18 at 04:07