mardi 20 juin 2017

Get value of a checkbox in another fragment

I would like to get the value of my checkbox that is in my confiFragment and get that value in MainActivity . The configFragment is related to the confiActivity.

I tried this method but i get a null ; can someone help me please?

This is my fragment:

public class configFragment extends android.support.v4.app.Fragment {
    CheckBox check;
    configActivity cmt;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.config_layout, container, false);
        check = (CheckBox) rootView.findViewById(R.id.checkbox1);
        TextView text1=(TextView) rootView.findViewById(R.id.text1);
        text1.setText("hello ");

        cmt=(configActivity) getActivity();
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(check.isChecked()){

                    Log.d("status","checked");
                    //check.setChecked(true);
                    cmt.checking=true;

                }else{
                    Log.d("status","not checked");
                    //check.setChecked(false);
                cmt.checking=false;
                }
            }
        });

        //cmt.checking=load();






        //  check();
        return rootView;
    }


    @Override
    public void onPause() {
        super.onPause();
        save(check.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        check.setChecked(load());
    }

    private void save(final boolean isChecked) {
        SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("check", isChecked);
        editor.commit();
    }

    public boolean load() {
        SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("check", check.isChecked());
    }

}

and this is the activity of this fragment:

    public class configActivity extends SingleFragmentActivity {

    public Boolean checking;

    @Override
    protected Fragment createFragment()
    {
        return new configFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configFragment cf=new configFragment();
        SharedPreferences sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
        checking=sharedPreferences.getBoolean("check", cf.check.isChecked());
    }
}

and this is my main activity :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView hello=(TextView) findViewById(R.id.hello);
    Button but1=(Button) findViewById(R.id.button1);


    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,configActivity.class);
            startActivity(i);

        }
    });

    hello.setText("Hello world");
   configActivity config=new configActivity();
    Log.d("status of checking",config.checking.toString());
    if(config.checking=true){
        Log.d("checked","true");
    }else{
        Log.d("not checked","false");
    }



}




Aucun commentaire:

Enregistrer un commentaire