Problem when calling fragment methods from another

0

I tell you that I have been doing a project from a tabbed activity, where the tabs are based on fragments. In one of the fragments I have three CheckBox elements, and what I want to do is disable these CheckBox from another fragment. I have tried two methods but none has worked for me. I leave the code of the methods I want to call.

public static CheckBox checkDate,checkTime,checkMemory;

public static void setCheckDate(boolean enable) {
    checkDate.setEnabled(enable);
}
public static void setCheckTime(boolean enable) {
    checkTime.setEnabled(enable);
}
public static void setCheckMemory(boolean enable) {
    checkMemory.setEnabled(enable);
}

The first method you use, in the other fragment.

SettingsFragment.setCheckDate(false);
SettingsFragment.setCheckTime(false);
SettingsFragment.setCheckMemory(false)

The second method you use:

SettingsFragment setCheckBox = (SettingsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_container);
setCheckBox.setCheckDate(false);
setCheckBox.setCheckTime(false);
setCheckBox.setCheckMemory(false);

Someone who can help me with this problem please. Thanks in advance

    
asked by Cesar Herbas 08.02.2018 в 22:19
source

1 answer

0

In the end I could find another way to disable the checkBox, without calling the methods this through SharedPreferences, the code I use is as follows.

In the fragment where the checkBox are located, disabling them will depend on the 'disable' flag,

    SharedPreferences disableCheckBox = getActivity().getSharedPreferences("disableCheckBox",Context.MODE_PRIVATE);
    boolean disabled = disableCheckBox.getBoolean("disableCheck",true);
    if(!disabled) {
        checkDate.setEnabled( false );
        checkTime.setEnabled( false );
        checkMemory.setEnabled( false );
    }

If in the other fragment we modify the value of 'disableCheck' to false, we can disable the checkBox for a second time to execute the fragment where they are.

SharedPreferences disableCheckBox = getActivity().getSharedPreferences("disableCheckBox",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = disableCheckBox .edit();
editor.putBoolean("disableCheck",false);
editor.commit();
    
answered by 10.02.2018 в 04:38