1

I have two activities, both have similar layout, i.e checkboxes. I want to sync the state of the checkboxes in both the activities. How do I do that?

Settings.class

    package com.example.myapp

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    public class Settings extends Activity {

    CheckBox checkBox_one  = null;
    CheckBox checkBox_two = null;
    CheckBox checkBox_three = null;
    CheckBox checkBox_four = null;
    CheckBox checkBox_five = null;

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

          //SAVE CHECKBOX STATE//

          checkBox_one = (CheckBox) findViewById(R.id.checkBox1);

          boolean isChecked = getBooleanFromPreferences("isChecked");
          Log.i("start",""+isChecked);
          checkBox_one.setChecked(isChecked);
          //checkBox_one.setChecked(true);//Enable By Default
          checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean",""+isChecked);
                Settings.this.putBooleanInPreferences(isChecked,"isChecked");
            }
          });

          checkBox_two = (CheckBox) findViewById(R.id.checkBox2);

          boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
          checkBox_two.setChecked(isCheckedTwo );
          //checkBox_two.setChecked(true);//Enable By Default
          checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
            }
          });

          checkBox_three = (CheckBox) findViewById(R.id.checkBox3);

          boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
          checkBox_three.setChecked(isCheckedThree );
          checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
            }
          });

          checkBox_four = (CheckBox) findViewById(R.id.checkBox4);

          boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
          checkBox_four.setChecked(isCheckedFour );
          //checkBox_four.setChecked(true);//Enable By Default
          checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
            }
          });

          checkBox_five = (CheckBox) findViewById(R.id.checkBox5);

          boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
          checkBox_five.setChecked(isCheckedFive );
          checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
            }
          });

        }

        public void putBooleanInPreferences(boolean isChecked,String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.commit();        
        }
        public boolean getBooleanFromPreferences(String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        Boolean isChecked = sharedPreferences.getBoolean(key, false);
        return isChecked;       

        }
        //-------------------------//


        @Override
        public void onBackPressed()
        {
            // Stop back button Functioning
        }

        public void openrate1(View view) { 
        Intent intent = new Intent(this, Rate.class);
        startActivity(intent);
        }

        public void gotohome(View view) { 
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        }

        public void savesettings(View view) { 
            Toast toast=Toast.makeText(this, "Settings successfully saved!", Toast.LENGTH_LONG); 
            toast.show();   
            }                       

        }

Progress.class

package com.example.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

public class Progress extends Activity {
    ProgressBar progressBar1;
    ProgressBar progressBar2;
    CheckBox checkBox1;
    CheckBox checkBox2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateProgressBars();
            }
        });

        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateProgressBars();
            }
        });
        }

        public void updateProgressBars() {

          progressBar1.setVisibility(View.GONE);
          progressBar2.setVisibility(View.GONE);

         if (checkBox1.isChecked() && checkBox2.isChecked()) {
            progressBar2.setVisibility(View.VISIBLE);
         } else if (checkBox1.isChecked()) {
            progressBar1.setVisibility(View.VISIBLE);
         }



    }


}
JRE.exe
  • 801
  • 1
  • 15
  • 28

3 Answers3

1

You can use SharedPreferences for this. In OnCreate() method of each and every Activity. You can check the value of SharedPreferences if it executes or not. If it executes then set the state of CheckBox according to it. you can set SharedPreference Value on CheckBox Event Listener OnCheckedChangeListener().

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
1

This is the problem of the communication between the two activities. For this, you can try this:

  1. SQLite database or Shared Preferences as Jatin Dudhat said.You can save data from Activity A, and get data from Activity B.
  2. Use startActivity(Intent),if you want to pass data from Activity A to B, you can use startActvity(Intent) in Activity A, and use intent with data and pass to Activity B.
  3. If you start Activity B in Activity A,and you want to get some data from Activity B when you com back to Activity A, you can use startActivityForResult(intent, requestCode);
  4. You can also use broadcast for the communication between the two activities.
  5. You can save data by the member variable of your class extends Application.

Not so exactly, but wish some help for you.

ifeegoo
  • 7,054
  • 3
  • 33
  • 38
0

Why don't you pass a boolean as intent.putExtra("checkValue", value);?

To be a little more specific:

In Activity A

Let's assume that ActivityA extends Activity

private boolean checkState = true; //Let's say you checkbox is currently checked.

public void sendCheckState(){
    Intent intent = new Intent(ActivityA.class, ActivityB.class);
    intent.putExtra("checkValue", value);
    startActivity(intent);
}

Now, you just call sendCheckState() after getting the boolean value.

In Activity B

You may call this code inside the onCreate() (We are still assuming that your ActivityB extends Activity)

Intent intent = getIntent();

Bundle extras = intent.getExtras();
if (extras != null) {

    if (extras.containsKey("checkValue")) {
        boolean checkValue = extras.getBoolean("checkValue", false);

        // TODO: Do something with the value.
    }
}
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • Can you post your code? I might be able to help you, but I need something to work on first. – Luis Lavieri Apr 15 '14 at 04:22
  • Ok. Let's say the `Activity` where your `CheckBox`es are is `ActivityA`. Now, if you see my answer, you may see that I am assuming a `boolean` with the value of one of the `CheckBox`es state (in my example: `true`). Then, if your `ActivityA extends Activity`, you may use `startActivity(intent);` in any part of that `ActivityA`. So, you should not be unable to implement `intent.` In that case, where are you trying to implement it? – Luis Lavieri Apr 15 '14 at 04:33
  • I edited my answer one more time. I tried to be as specific as I could. – Luis Lavieri Apr 15 '14 at 04:39
  • You already chose @Jatin Dudhat suggestion. Take a look at [this](http://stackoverflow.com/questions/10480735/sharedpreferences-for-a-certain-item-from-a-listactivity&cd=1&ved=0CCYQFjAA&usg=AFQjCNGvoizBnyvz5FsVxIgzPZ-3-HqKyw) answer. – Luis Lavieri Apr 15 '14 at 05:16
  • But, in your code you are implementing `SharedPreferences`. Check the link please. – Luis Lavieri Apr 15 '14 at 05:22
  • i have checked it but am not getting it, actually the problem is i used to have both the checkboxes and progressbars in a single activity and also both the codes , but the problem is they both won't work properly, checkboxes behaved weird and also not saving state , so i decided to choose this method, can you get both the codes to work in a single activity? – JRE.exe Apr 15 '14 at 05:26
  • Tomorrow I'll help you out. It's already 2am where I am, and I am using my phone. So, is not easy for me to edit code. Try passing the content of the xml layout of the second activity to the first one and work from there. Sorry that I am not being very helpful. – Luis Lavieri Apr 15 '14 at 05:30
  • i find one solution http://stackoverflow.com/questions/15111266/how-to-store-check-box-value-from-one-activity-to-another-activity please check the last answer (@vishesh chandra's answer) – JRE.exe Apr 15 '14 at 16:01
  • could you figure it out? please help, i'm really stuck! – JRE.exe Apr 20 '14 at 07:35
  • just tell me how to implement your code? "new Intent(ActivityA.class, ActivityB.class);" is showing error The constructor Intent(Class, Class) is undefined. – JRE.exe Apr 20 '14 at 08:01
  • Sorry, but what you are asking should be posted in a new question. It is a different topic. – Luis Lavieri Apr 20 '14 at 13:49
  • it's not a Different topic . i'm on the same topic!. Anyways, i am about to get this Resolved – JRE.exe Apr 20 '14 at 13:52
  • Aren't you trying to put both classes in a single class, are you? – Luis Lavieri Apr 20 '14 at 13:53
  • that was just an idea so that the issue could be resolved , i dont mind how its resolved , i just want it resolved – JRE.exe Apr 20 '14 at 14:14