I'm trying to save all the values & clicks from user during screen rotation, but my button click and text view isn't working. I've spent so many hours trying to figure out what to write inside this method and none of them worked. The input from Select your Tip button and textView output isn't being saved at the moment.
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = Integer.parseInt(text);
outState.putInt("count", no);
String num_of_splits = txtNumOfSplits.getText().toString();
//Convert String to Integer
int no2 = Integer.parseInt(num_of_splits);
outState.putInt("count", no2);
money = Double.parseDouble(edtBillAmount.getText().toString());
outState.putString("bill", String.valueOf(money));
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState: Started");
String text = txtNumOfSplits.getText().toString();
int no = savedInstanceState.getInt("count");
txtNumOfSplits.setText(no + "");
money = Double.parseDouble(edtBillAmount.getText().toString());
String money = savedInstanceState.getString("bill");
txtTotalBill.setText("Total Bill\n$" + finalAmount.toString());
}
btn_add = findViewById(R.id.btn_add);
btn_subtract = findViewById(R.id.btn_subtract);
btn_20 = findViewById(R.id.btn_20);
btn_15 = findViewById(R.id.btn_15);
btn_10 =findViewById(R.id.btn_10);
btn_calculate = findViewById(R.id.btn_calculate);
btn_reset = findViewById(R.id.btn_reset);
txtBill = findViewById(R.id.txtBill);
txtTip = findViewById(R.id.txtTip);
txtSplit = findViewById(R.id.txtSplit);
txtRoundUp = findViewById(R.id.txtRoundUp);
txtNumOfSplits = findViewById(R.id.txtNumOfSplits);
txtTotalHeading = findViewById(R.id.txtTotalHeading);
txtTotalPerPerson = findViewById(R.id.txtTotalPerPerson);
txtTotalBill = findViewById(R.id.txtTotalBill);
txtTotalTip = findViewById(R.id.txtTotalTip);
txt_switch_error = findViewById(R.id.txt_switch_error);
switchRoundUp = (Switch) findViewById(R.id.switchRoundUp);
switchRoundDown = (Switch) findViewById(R.id.switchRoundDown);
edtBillAmount = findViewById(R.id.edtBillAmount);
horizontalLine = findViewById(R.id.horizontalLine);
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);
}