0

I would like to get the booleanvalue of my checkbox that is in my Fragment and get that value in MainActivity . The Fragment is related to an Activity. Fragment:

@Override
    public void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }


}

Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new MyPreferenceFragment()).commit();

Can someone please explain to me how i make it. I just tried with an Interface but i got always a null error like the checkbox isnt initialize

resroot

Mat
  • 1,960
  • 5
  • 25
  • 38
  • You should look into how to define and use callbacks [Look here](https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android) – Haroun Hajem Nov 13 '18 at 16:32

2 Answers2

0

I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence). findPreference returns a Preference object that would give you values.

Gaurav Vashisth
  • 7,547
  • 8
  • 35
  • 56
0

This is how you bind and get the value of a checkbox :

Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
boolean checkboxState = cb.isChecked();

now in order to pass that information to your activity with callbacks, you do the following:

in your fragment create an interface:

public interface MyInterface {
    public void onMyInterface(boolean b);
}

in the onAttach() method include the following:

MyInterface myInterface;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    myInterface = (MyInterface) context;
}

when you need to send the data to the activity, within your fragment add the following:

sendBooleanCheckBox(checkBoxState);

where:

public void sendBooleanCheckBox(boolean b) {
    myInterface.onMyInterface(b);
}

and in your activity:

@Override
public void onMyInterface(boolean b) {
    Log.d("TAG","check box value is: " + b);
}

make sure that your activity implements the interface:

public MyActivity implements OnMyInterface {...
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing – Marco Schwarz Nov 13 '18 at 17:59
  • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) { checkBoxValue = b;} and use that value from there. – Nikos Hidalgo Nov 14 '18 at 09:40