简体   繁体   中英

Passing checkbox input from one fragment to another fragment in the same activity

I have fragment A with checkboxes and fragment B with EditText to write.

I want to disable the Edittext of fragment B when Fragment A checkbox is checked.

Y tried with Shared Preferences but it's does not disable nothing.

In Fragment A:

CheckBox.setChecked(client.getUCheckbox);

CheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean b) {
            if (b){       
             
                SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPref.edit();
                edit.putBoolean("CheckBox", true);
                edit.apply();
            }

In fragment B:

 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    realm = Realm.getDefaultInstance();
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    sharedPref.getBoolean("Checkbox",false);

}

 @Override
public View onCreateView(
        @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_determinaciones_parte_aguas, container, false);
    ButterKnife.bind(this, root);

    rellenarVista();

    return root;
}

 private void rellenarVista() {
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    sharedPref.getBoolean("CheckBox",false);

    if (CheckBox){
        disableEditText();
    }

disableEditText is a method that set enable as false to all the editText.

The solution I tried is from this post.

Passing Checkbox input from one fragment to another

Thank you in advance.

EDIT: your disableEditText(); method is only called once in fragment B's onCreateView() , when the fragment is created. You should pass a reference to fragment B to fragment A (or the parent activity) and update the check box directly in fragment A's onCheckedChanged()

The preferences you are using are private to the requesting activity.

You need to store your boolean in preferences accessible from both activities. Instead of

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

you can use:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Note: your onCheckedChanged() implementation does not update the flag if the checkbox is unchecked. Here is a fix:

public void onCheckedChanged(CompoundButton buttonView, boolean b) {

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor edit = sharedPref.edit();
    edit.putBoolean("CheckBox", b);
    edit.apply();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM