5

Is there any way by which I can save the state of my checkboxes (checked or unchecked) when user exits the application so that I can reload this state when the application restarts?

@Override
public void onPause()
{

    super.onPause();
    save(itemChecked);
}
@Override
public void onResume()
{
    super.onResume();
    checkOld = load();

    for (int i = 0 ; i < checkOld.length; i++)
    {
        notes.ctv.get(i).setChecked(checkOld[i]);
    }
}
@Override
public void onRestart()
{
    super.onResume();
    checkOld = load();

    for (int i = 0 ; i < checkOld.length; i++)
    {
        notes.ctv.get(i).setChecked(checkOld[i]);
    }
}

private void save(final boolean[] isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
 insertState();
 for(Integer i = 0; i < isChecked.length; i++)
 {
     editor.putBoolean(i.toString(), isChecked[i]);
 }
editor.commit();
}

private boolean[] load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    boolean [] reChecked = new boolean[itemChecked.length];
    for(Integer i = 0; i < itemChecked.length; i++)
    {
         reChecked[i] = sharedPreferences.getBoolean(i.toString(), false);
    }
    return reChecked;
}
gonzobrains
  • 7,198
  • 11
  • 73
  • 126
Varundroid
  • 8,802
  • 13
  • 61
  • 90

2 Answers2

21

Combine onPause() and onResume() to save and load your CheckBox value.

Sample code:

@Override
public void onPause() {
    super.onPause();
    save(mCheckBox.isChecked());
}

@Override
public void onResume() {
    super.onResume();
    mCheckBox.setChecked(load());
}

private void save(final boolean isChecked) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

private boolean load() { 
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", false);
}
Wroclai
  • 26,380
  • 7
  • 72
  • 67
  • @Viktor i treid your solution but as you can see i mentioned CheckBoxes so i changed your code little bit to make it to accpet array of boolean but still i am getting all the checkboxes unchecked though i checked few of them before closing the application. Any idea why? i even tried puting onResume code in onRestart function but its still not working. please help. – Varundroid Apr 17 '11 at 11:39
  • @Viktor done. now please check where i am doing wrong. .i have added my adapter's code too so you can understand my work easily. and in above code notes is an instance of my adapter.i have one more question do i need to perform something extra in my onCreate function like checking if there is any SharedPreference then load it or something like that – Varundroid Apr 17 '11 at 11:50
  • @Varundroid: `onResume()` is called after `onCreated()` and therefore is checking in `onCreate()` unnecessary. What I can see in your code, you're loading and saving correctly. Are you sure have the correct id's of your `CheckBox`es? – Wroclai Apr 17 '11 at 12:33
  • @Viktor yeah i hope so because this is the way i am storing values in my boolean arrya :- public boolean[] insertState() { int size = notes.getCount(); for(int i = 0; i < size; i++) { itemChecked[i] = notes.itemChecked.get(i); } return itemChecked; } – Varundroid Apr 17 '11 at 13:26
0

i believe the google notepad3 tutorial explains about saving and restore state http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html

save the state bundle in onSaveInstanceState() then get the bundle back in onStart()

Hope that helps

Edit: also check this one, its more concise. onSaveInstanceState () and onRestoreInstanceState ()

Community
  • 1
  • 1
wired00
  • 12,270
  • 6
  • 65
  • 65