0

I have the following code implemented but it doesnt work like I want:

My onSaveInstanceState and restore:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putIntArray("ColorArray", colorArraySave);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    colorArraySave = savedInstanceState.getIntArray("ColorArray");
}

My onStop, onResume, onPause methods:

@Override
protected void onPause() {
    super.onPause();
    saveGridViewColor(colorArraySave);
}

@Override
protected void onStop() {
    super.onStop();
    saveGridViewColor(colorArraySave);
}

@Override
protected void onResume() {
    super.onResume();
    colorTheGridView(colorArraySave);
}

So I have an int array that I want to save if the activity is stopped/closed and load back instanly if the activity is called again but the code I use doesnt work.

  • saveGridViewColor() is saving the int[] array to colorArraySave
  • colorTheGridView() is coloring the gridview items via an int array.

Any ideas how can I get this working?

d05hjkl
  • 39
  • 7
  • Do you have destroy activities turned on in your phone or emulator's developer preferences? You don't need to save in onPause() and onStop(). Saving in onPause() is enough. Also, after onPause() you will go through onSaveInstanceState(), but when coming back into onResume() you will not get a "bundle." You only need the bundle in onCreate() (or onRestoreInstanceState()), but these are not called when resuming. Your data should still be in memory. If you put a simple Log.d(TAG, "{method name here}"); you will see that demonstrated. – Bill Mote Mar 28 '15 at 13:35
  • So, what work are you doing in onCreate() that you are not doing in onResume()? There may be things you need to move in order to maintain your application state. – Bill Mote Mar 28 '15 at 13:37
  • 2
    `savedInstanceState` is a `Bundle`. Use `SharedPreferences` instead. Refer this thread http://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android – priyank Mar 28 '15 at 13:53
  • Also see this http://stackoverflow.com/a/22222506/3498044 – priyank Mar 28 '15 at 13:55
  • @BillMote: So I have to load back the colorArraySave in onCreate, or I misundertood you? – d05hjkl Mar 28 '15 at 14:12
  • @Ryan: sharedpref isnt good for me I guess because it saves the state when I close the app aswell but I need to save only when i.e. I press back to the main menu. – d05hjkl Mar 28 '15 at 14:12
  • Nope. Look at an activity's lifecycle. onPause() will result in an onSaveInstanceState() call, but that's a "just in case" call. onResume() has no bundle --> nor do any of the methods following it before your activity is visible. Unintuitive for sure. What I'm saying is: you are probably doing something in onCreate() to setup your view that you need to move to onResume(). – Bill Mote Mar 28 '15 at 14:19
  • Yes, in onCreate() I create a gridview with a custom adapter. In custom adapter getView I color the items, thats all.. so how should I get this worked in onResume()? – d05hjkl Mar 28 '15 at 14:36

0 Answers0