user2147397
user2147397

Reputation:

checkbox value in other activity

hy, everyone i'm searching for a few days a solution for one problem i tried to search but i didn' find an answer for my problem so i decided to ask you maybe someone it will so kind and wil explaine to me ho can solve my problem.

So i have two activities, a configure one and a main one, my problem is that when i check one checkbox in the configure activity (i succeed to save the checkbox state so when i check it will be checked even if i leave the configure activity), but i don't know how can i use the checkbx state in the main activity. i found many suggestions but for for me nothing worked. thank you in advance.

thank you for all for the answers but i not succeeded in this way to solve my problem, and with your suggestions i did the following: in the second activity(where i have the CheckBox): i have one Save function where i save with SharedPreferences the Checkbox State( even if i close the activity or anything else the state of Checkbox it's keeping his state) and i have made a Load function where when i load back the activity it's loading the previous state so inside this activity i thing it is ok, But in this case how can use the load function's value (in fact the checkbox value) int the MainActivity. I'm sorry for inconvenience but i'm knew but i would like to learn.

Thank you ones again.

Upvotes: 0

Views: 914

Answers (3)

Ajay S
Ajay S

Reputation: 48602

Store the value in SharedPreference to get the value of checkbox

chkIos.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (((CheckBox) v).isChecked()) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putString("checkboxState",((CheckBox) v).isChecked());
            editor.commit();
            //Save the state of checkbox in sharedPreference
        }
    }
});

In main activity get the state like this.

SharedPreferences sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
String stateValue = sharedPreferences.getString("checkboxState);

Upvotes: 4

Andres
Andres

Reputation: 6200

You can use SharedPreferences to save the state of your CheckBox in the device so you can retrieve it from another Activity.

Here is an example how to use them. Also you can check this YouTube video that shows you how to make preferences screen for your Activities.

Upvotes: 0

steevoo
steevoo

Reputation: 631

i guess the sharepreferences will resolve that

SharedPreferences.Editor editor = app_preferences.edit();
editor.putBoolean("Checkbox", value);
editor.commit();


SharedPreferences app_preferences = 
    PreferenceManager.getDefaultSharedPreferences(this);
boolean value = app_preferences.getBoolean("Checkbox", false);

Upvotes: 0

Related Questions