0

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.

3 Answers3

4

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);
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • hy tried but i got an error at this point "context.getSharedpreferences..." please if you have any idee what i did wrong please help me thank you –  Mar 25 '13 at 07:33
  • Declare the context variable in activity like `Context context;` and assign value in this onCreate like `context = this;` – Ajay S Mar 25 '13 at 07:37
  • thank you now it seems to be good so i can read the value from the second activity but the problem is that when i leave the activity with the checkbox the status of checkbox it is not staying checked and when i read the value in the first activity i can read only one state indifferently if the checkbox is checked or not thank you for your help –  Mar 25 '13 at 08:56
  • or sorry for disturbing you again for saving the checkbox state i did the following: to load the value on onCreate i did a Load function, for save this value i did a save function, so when i set a chekbox i save its value and when i leave respectivly come back to this activity i load the value and it is still pressed so i thing in thsi way i succeded to solve this problem but in this case how can i use from function Load the checkbox Boolean value in the other activity thanko you again and i'm sorry to disturbing you i'm waiting for you answer –  Mar 25 '13 at 09:14
  • @adt I told you above with example. Please implement using like that. – Ajay S Mar 25 '13 at 16:21
0

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);
steevoo
  • 621
  • 6
  • 18
  • hy i tried to implement your suggestion but the state of checkbox is not saved when i leave the ativity and in main activity where i tried to check its state is false, could you give me soem suggestion how should i do this to work... thank you. –  Mar 25 '13 at 07:31
0

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.

Community
  • 1
  • 1
Andres
  • 6,080
  • 13
  • 60
  • 110