Onsaveinstancestate () and Onrestoreinstancestate ()

How to use onSaveInstanceState and onRestoreInstanceState to prevent losing checkbox and radiobutton selections

Use something like this: (adjust to your own code)

public class YourActivity extends AppCompatActivity {

private static final String CHECKBOX_CHECKED_KEY = "is_checkbox_checked";

private CheckBox myCheckBox;

@Override
protected void onCreate(Bundle savedInstanceState) {

...

if (savedInstanceState != null) {

myCheckBox.setChecked(savedInstanceState.getBoolean(CHECKBOX_CHECKED_KEY));
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState)
outState.putBoolean(CHECKBOX_CHECKED_KEY, myCheckBox.isChecked());
}
}

You will need to define different keys for each CheckBoxe's state you want to save.

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

Per the documentation:

void onRestoreInstanceState (Bundle savedInstanceState)

This method is called between onStart() and onPostCreate(Bundle).

void onSaveInstanceState (Bundle outState)

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

Where use onSaveInstanceState and onRestoreInstanceState methods?

1 - The first article implements these methods on a layoutManager!?
So, I'm using a default GridLayoutManager, so to implement save and
restore instance in the GridLayoutManager I should create my own class
extending the default class?

If you look at both the article and the SO post they do not implement anything inside the LayoutManager, they just use methods that already exist.
If you look in the documentation page for the GridLayoutManager there are already both a onSaveInstanceState() and a onRestoreInstanceState (Parcelable state) methods (these two methods are the "convenient API" the blog mentions at the start).

As I'm sure you've noticed GridLayoutManager inherits from LinearLayoutManager
official documentation of LinearLayoutManager.onSaveInstanceState():

Called when the LayoutManager should save its state. [...] Returns:
Parcelable Necessary information for LayoutManager to be able to restore its
state

official documentation of LinearLayoutManager.onRestoreInstanceState (Parcelable state)
Very incomplete but you can tell that it uses the same Parcelable parameter returned by LinearLayoutManager.onSaveInstanceState()

2 - I can implement these methods in the layoutmanager regardless of
implementing them in the activity?

To be clear: no need to re-implement the LayoutManager. I don't see why you would need to do that, the methods are there and ready to use.
The Activity methods of the same name are what you need to implement.

3 - Where is the correct place to implement these methods? or is there
a official answer to the question: "How restore the state of a
recyclerview?"

The correct place to do this is the Activity's lifecycle methods, these will be called at the appropriate times to save and restore your LayoutManager state.
Quoting the SO answer you mentioned:

//---> this is the Activity's onSaveInstanceState
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);

// Save list state
mListState = mLayoutManager.onSaveInstanceState();
state.putParcelable(LIST_STATE_KEY, mListState);
}

//---> this is the Activity's onRestoreInstanceState
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);

// Retrieve list state and list/item positions
if(state != null)
mListState = state.getParcelable(LIST_STATE_KEY);
}

//---> this is the Activity's onResume
@Override
protected void onResume() {
super.onResume();

if (mListState != null) {
mLayoutManager.onRestoreInstanceState(mListState);
}
}

I haven't seen any official documentation about specifically restoring the RecyclerView/LayoutManager state but the lifecycle subject is a very important one in Android. I believe that after fully understanding this one can make the right decisions regarding specific use-cases.

Hope this helps ;)

How to fix my onSaveInstanceState and onRestoreInstanceState?

Is that what you need? You can add double values parsing if necessary.

    private static final String NUM_OF_SPLITS_KEY = "num_of_splits_key";
private static final String SELECTED_BUTTON_KEY = "selected_button_key";
private static final String TOTAL_PER_PERSON_KEY = "total_per_person_key";
private static final String TOTAL_BILL_KEY = "total_bill_key";
private static final String TOTAL_TIP_KEY = "total_tip_key";

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");

String textNumOfSplits = txtNumOfSplits.getText().toString();
int numOfSplits = Integer.parseInt(textNumOfSplits);
outState.putInt(NUM_OF_SPLITS_KEY, numOfSplits);

int selectedButtonId = 0;

if (btn_10.isSelected()) selectedButtonId = R.id.btn_10;
else if (btn_15.isSelected()) selectedButtonId = R.id.btn_15;
else if (btn_20.isSelected()) selectedButtonId = R.id.btn_20;

outState.putInt(SELECTED_BUTTON_KEY, selectedButtonId);

outState.putString(TOTAL_PER_PERSON_KEY, txtTotalPerPerson.getText().toString());
outState.putString(TOTAL_BILL_KEY, txtTotalBill.getText().toString());
outState.putString(TOTAL_TIP_KEY, txtTotalTip.getText().toString());

}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");

int numOfSplits = savedInstanceState.getInt(NUM_OF_SPLITS_KEY);
txtNumOfSplits.setText(numOfSplits + "");

String selectedButtonId = savedInstanceState.getInt(SELECTED_BUTTON_KEY);

if (selectedButtonId == btn_10.getId()) selectButton(btn_10);
else if (selectedButtonId == btn_15.getId()) selectButton(btn_15);
else if (selectedButtonId == btn_20.getId()) selectButton(btn_20);

String totalPerPerson = savedInstanceState.getString(TOTAL_PER_PERSON_KEY);
txtTotalPerPerson.setText(totalPerPerson);

String totalBill = savedInstanceState.getString(TOTAL_BILL_KEY);
txtTotalBill.setText(totalBill);

String totalTip = savedInstanceState.getString(TOTAL_TIP_KEY);
txtTotalTip.setText(totalTip);
}

private void selectButton(@NonNull Button button) {
button.setSelected(true);
}


Related Topics



Leave a reply



Submit