0

I have some trouble with setting a checkbox to be unchecked. I have two fragments, first fragment contains a checkbox and a button, after I check the check box and press the next button, second fragments loads and displays a message. When I press back button, the first fragment reloads and the checkbox remains checked, how can I make it to be unchecked.

Here's how I initialize the checkbox, this function is called from onViewCreated()

private void initRefundCheckBox(){

        final CheckBox refund = getView().findViewById(R.id.refund_checkbox);

        if(isRefund)//This section is to check if we reloaded the first 
                    //pragment and isRefund was already true - then set it 
                    //to be false and uncheck the checkbox
        {
            isRefund = false;
            refund.toggle();
            refund.setSelected(isRefund);
            refund.setChecked(isRefund);
        }
        refund.setChecked(isRefund);
        refund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                isRefund = refund.isChecked();
                /// somecode

            }
        });
    }
Keselme
  • 3,779
  • 7
  • 36
  • 68

2 Answers2

1

Override a function in the first fragment .. setUserVisibilityHint(...) in there just set the check to false.. like this..

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (getActivity()!=null)
    {

        refund.setChecked(false);


    }
}
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

Simply, after you start the second fragment on onClickListener of the nextButton, reset the checkbox.

refund.setChecked(false);
Fady Emad
  • 173
  • 2
  • 11
  • what if he checked it and then come back again by pressing the back button. it will make it unChecked.... I don't know how he accepted it as answer. may be he is not aware of all scenarios – Zaid Mirza Feb 25 '18 at 09:11
  • @ZaidMirza he said in the question **When I press back button, the first fragment reloads and the checkbox remains checked, how can I make it to be unchecked.** i think you missed this sentence. – Fady Emad Feb 25 '18 at 09:19