0

I am creating radiobuttons in a radiogroup dynamically. When using the saved instance state to set which radiobutton was selected before the app shut down the correct radiobutton gets checked, but it doesn't display the checked state. By logging data I know that it is checked. It is also apparent as it doesn't update when clicked.

When I set the radiobutton that is checked at creation when not recreating the instance state there is no problem.

public class PaymentMethodsAdapter {
private static final String TAG = PaymentMethodsAdapter.class.getCanonicalName();

private LayoutInflater layoutInflater;
private RadioGroupPlus radioGroup;
private List<PaymentMethod> paymentMethods;
private int selectedPaymentMethod = -1;

public PaymentMethodsAdapter(RadioGroupPlus radioGroup, Context context) {
    layoutInflater = LayoutInflater.from(context);
    this.radioGroup = radioGroup;
    radioGroup.setOnCheckedChangeListener(new RadioGroupPlus.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroupPlus radioGroupPlus, int i) {
            selectedPaymentMethod = i;
        }
    });
}

public void setPaymentMethods(List<PaymentMethod> paymentMethods, int selectedPaymentMethod) {
    this.selectedPaymentMethod = selectedPaymentMethod;
    this.paymentMethods = paymentMethods;
    radioGroup.removeAllViews();
    for(int i = 0; i < paymentMethods.size(); i++) {
        PaymentMethod paymentMethod = paymentMethods.get(i);
        View v = layoutInflater.inflate(R.layout.view_payment_method, radioGroup, false);
        RadioButton radioButton = v.findViewById(R.id.paymentMethodTitle);
        radioButton.setId(i);
        radioButton.setText(paymentMethod.getName());
        TextView description = v.findViewById(R.id.paymentMethodDescription);
        description.setText(paymentMethod.getDescription());
        radioGroup.addView(v);
    }
    if(selectedPaymentMethod >= 0) {
        radioGroup.check(selectedPaymentMethod);
    }
}

public int getSelectedPaymentMethodPosition() {
    return selectedPaymentMethod;
}

public PaymentMethod getSelectedPaymentMethod() {
    if(selectedPaymentMethod >= 0) {
        return paymentMethods.get(selectedPaymentMethod);
    } else {
        return null;
    }
}
}

in fragment:

private void updatePaymentMethods() {
    if(viewInflated && checkoutInfo != null) {
        paymentMethodsAdapter.setPaymentMethods(checkoutInfo.getPaymentMethods(), selectedPaymentMethod);
        paymentMethodsGroup.invalidate();
        Log.i(TAG, "updating payment methods. selected: "+ selectedPaymentMethod);
    }
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    if(savedInstanceState != null) {
        selectedPaymentMethod = savedInstanceState.getInt(SELECTED_PAYMENT_METHOD_KEY, -1);

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_checkout, container, false);
    ...
    viewInflated = true;

    updatePaymentMethods();

    return v;
}
elin
  • 71
  • 1
  • 2
  • How do you save data with a key `SELECTED_PAYMENT_METHOD_KEY`? – Paresh P. Apr 09 '18 at 06:26
  • @Override public void onSaveInstanceState(@NonNull Bundle outState) { outState.putInt(SELECTED_PAYMENT_METHOD_KEY, paymentMethodsAdapter.getSelectedPaymentMethodPosition()); super.onSaveInstanceState(outState); } – elin Apr 10 '18 at 05:48

0 Answers0