3

I am using a class with static values called DB (for Data Base) in my application. When I first run the app, a static byte array from this class is filled and used. Then, when I partially close my app (not closing it definitily just put in background) if a reopen it after 20 seconds more or less, the value of the variable is still here but if I let my app in the background for more than 1 minute the value turns to null.

How can I avoid this to happen?

Roman Panaget
  • 1,578
  • 12
  • 21

3 Answers3

1

store your variable value to shared preferences and load the value from shared preferences in the onResume() Method of activity and store the value in the onPause() Method.

Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
1

Handling lifestyle events properly is an important aspect of Android development.

I suggest that you read the following to make sure that you understand what happens to your app when you turn off your screen, change to another application or any other action that might change the state of your app:

http://developer.android.com/training/basics/activity-lifecycle/index.html

My suggestion is to store your data by overriding onSaveInstanceState() like so:

@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 on your onCreate(), you can reload it like so:

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

I hope that this helps!

Good luck in your future developing!

Adam
  • 472
  • 1
  • 7
  • 14
  • This is very good but your answer doesn't fit completely to my problem. My byte[] must be null when running the app and must be the same in all the activities, how can I handle this situation with your method? Because you're saying that I must set the value of the byte array in the onCreate method, but in my onCreate method my byte array is null, its value is set a bit later. Once set, it is shared with the other activities and when the app is killed, the byte[] must be save as null. – Roman Panaget Aug 22 '15 at 08:18
0

I found a solution with the help of the commenters.

For those who had the same problem:

Copy this in all your Activities to ensure that the data is constantly updated in the preferences:

@Override
public void onPause(){
    super.onPause();
    String bytearray = Base64.encodeToString(DB.bytearray, Base64.DEFAULT);
    prefs.edit().putString("BYTEARRAY", bytearray).apply();
}
@Override
public void onResume(){
    super.onResume();
    String bytearray = prefs.getString("BYTEARRAY", Base64.encodeToString(DB.bytearray, Base64.DEFAULT));
    DB.bytearray = Base64.decode(bytearray, Base64.DEFAULT);
}

Then, add this code in all your Activities to ensure that the values are not saved when you close your app.

@Override
public void onDestroy(){
    super.onDestroy();
    String bytearray = "";
    prefs.edit().putString("BYTEARRAY", bytearray).apply();
}
Roman Panaget
  • 1,578
  • 12
  • 21