3
public class StartActivity extends AppCompatActivity {

    private boolean addition=true;
    private boolean subtraction=false;
    private boolean multiplication=false;
    private boolean division=false;

    public static final String ADDITION = "";
    public static final String SUBTRACTION = "";
    public static final String MULTIPLICATION= "";
    public static final String DIVISION = "";

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

        final Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);

        CheckBox additionCheckBox = (CheckBox)findViewById(R.id.additionCheckBox);
        CheckBox multiplicationCheckBox = (CheckBox)findViewById(R.id.multiplicationCheckBox);
        CheckBox divisionCheckBox = (CheckBox)findViewById(R.id.divisionCheckBox);
        CheckBox subtractionCheckBox = (CheckBox)findViewById(R.id.subtractionCheckBox);

        Button playButton = (Button)findViewById(R.id.playButton);

        additionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    addition = true;
                }else{
                    addition = false;
                }
            }
        });

        multiplicationCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                   multiplication = true;
                }else{
                    multiplication= false;
                }
            }
        });

        subtractionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    subtraction = true;
                }else{
                    subtraction = false;
                }
            }
        });

        divisionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    division = true;
                }else{
                    division = false;
                }
            }
        });

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!addition && !subtraction && !multiplication && !division){
                    final Dialog dialog = new Dialog(StartActivity.this);
                    dialog.setContentView(R.layout.custom_dialog);
                    dialog.setTitle("My custom dialog");

                    //set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog.findViewById(R.id.textView);
                    text.setText("Android custom dialog example!");


                    Button button = (Button) dialog.findViewById(R.id.button);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        dialog.dismiss();
                        }
                    });

                    dialog.show();
                } else{
                    Intent i = new Intent(StartActivity.this, Main2Activity.class);
                    i.putExtra(ADDITION,addition);
                    i.putExtra(SUBTRACTION,subtraction);
                    i.putExtra(MULTIPLICATION,multiplication);
                    i.putExtra(DIVISION,division);
                    startActivity(i);
                }
            }
        });
}

This is the starting activity and I am trying to sent to the next activity the 4 boolean variables one for each basic math operation to see which one the user has checked. but they are always false in the next activity and i dont know why. here is the next activity:

public class Main2Activity extends AppCompatActivity {

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

        final Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);

        Intent i = getIntent();
        Bundle b = i.getExtras();
        boolean addition = b.getBoolean("ADDITION");
        boolean subtraction = Boolean.getBoolean("SUBTRACTION");
        boolean multiplication = b.getBoolean("MULTIPLICATION");
        boolean division = b.getBoolean("DIVISION");

        int operatorCounter =0;

        TextView additionTextView = (TextView) findViewById(R.id.additionTextView);
        TextView subtractionTextView = (TextView) findViewById(R.id.subtractionTextView);
        TextView multiplicationTextView = (TextView) findViewById(R.id.multiplicationTextView);
        TextView divisionTextView = (TextView) findViewById(R.id.divisionTextView);

        if (addition) {
            additionTextView.setText("addition:checked");
        }else{
            additionTextView.setText("addition:not checked");
        }

        if (subtraction) {
            subtractionTextView.setText("subtraction:checked");
        }else{
            subtractionTextView.setText("subtraction:not checked");
        }

        if (multiplication) {
            multiplicationTextView.setText("multiplication:checked");
        }else{
            multiplicationTextView.setText("multiplication:not checked");
        }

        if (division) {
           divisionTextView.setText("division:checked");
        }else{
          divisionTextView.setText("division:not checked");
        }   
    }
}

I am setting the text in the textbox to see if the value is passed correctly but is always false. Sorry for bad code I'm quite new in android, actually is the forth mini project in android and its school homework.

Robert P
  • 9,398
  • 10
  • 58
  • 100
Giannis Savvidis
  • 692
  • 4
  • 13
  • 34
  • 2
    Refer: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – Satan Pandeya Dec 28 '16 at 12:00
  • 1
    Welcome, this is a lot of line of code just to create an intent to pass a Boolean. You should see how to write a [mcve]. But I think you would find answer on SO by searching a bit – AxelH Dec 28 '16 at 12:00

8 Answers8

4

The problem I see are the id's:

public static final String ADDITION = "";
public static final String SUBTRACTION = "";
public static final String MULTIPLICATION= "";
public static final String DIVISION = "";

this should be something and different like:

public static final String ADDITION = "ADDITION";
public static final String SUBTRACTION = "SUBTRACTION";
public static final String MULTIPLICATION= "MULTIPLICATION";
public static final String DIVISION = "DIVISION";

and use the same values for retrieving the values in the other activity

petrumo
  • 1,116
  • 9
  • 18
  • 1
    You should add that since he create constants, OP should use them to get the values in the intent too. – AxelH Dec 28 '16 at 12:04
3

Try this code for both sending and getting values.

Pass to first Activity:

Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i);

Retrieve in Second Activity:

Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");
Laurel
  • 5,965
  • 14
  • 31
  • 57
Apoorv Mehrotra
  • 607
  • 5
  • 13
  • isn't that what the posted code in the question actually do? –  Dec 28 '16 at 11:59
  • i didn't understand what you said please explain... – Apoorv Mehrotra Dec 28 '16 at 12:00
  • @RC to be honnest, you need to find the line first ;) but as petrumo said, the problem is from the keys – AxelH Dec 28 '16 at 12:03
  • @AxelH yes the key issue is a good catch I agree, and this answer would have been great if there was some explanation (I think the OP know how to do it, his/her code just have some issue with the key) –  Dec 28 '16 at 12:04
  • @RC, To be fair, the question would have been solve by creating the [mcve], this would have been obvious. – AxelH Dec 28 '16 at 12:14
  • thank you guys, had quite few mistakes but i found it out – Giannis Savvidis Dec 28 '16 at 15:57
1

Its because you are not setting value in globle variable, so you need to set first globle variable value like this

public static final String ADDITION = "ADDITION";
public static final String SUBTRACTION = "SUBTRACTION";
public static final String MULTIPLICATION= "MULTIPLICATION";
public static final String DIVISION = "DIVISION";

then set it

Intent i = new Intent(StartActivity.this, Main2Activity.class);
                i.putExtra(ADDITION,addition);
                i.putExtra(SUBTRACTION,subtraction);
                i.putExtra(MULTIPLICATION,multiplication);
                i.putExtra(DIVISION,division);
                startActivity(i);

than aceess value from another activity like this

 boolean addition = getIntent().getExtras().getBoolean("ADDITION");
 boolean sub = getIntent().getExtras().getBoolean("SUBTRACTION");
 boolean mult = getIntent().getExtras().getBoolean("MULTIPLICATION");
 boolean division = getIntent().getExtras().getBoolean("DIVISION");
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
1

Make sure you are using same key to Put and to Get data.

Also update

boolean subtraction = Boolean.getBoolean("SUBTRACTION");

To

boolean subtraction = b.getBoolean("SUBTRACTION");
Rahul
  • 10,457
  • 4
  • 35
  • 55
1
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("BoolName", true);
startActivity(intent );

Retrieve boolean value from intent :

 Boolean yourBool = getIntent().getExtras().getBoolean("BoolName");
Sanjeev Sangral
  • 1,385
  • 2
  • 25
  • 37
1

I will use this post to show why a mcve is useful since this is a perfect example, I will use his code, cleaning the useless part and we will quickly find the problem, I don't deserve or ask for upvote but don't downvote either, if you are against this answer, comment and I will remove it if need.

So, let clean the code, I am only interested in the Intent creation here. So let clean the rest and keep the variables/constants used. To be minimal, we only need one boolean, let's keep the first putBoolean.

public class StartActivity extends AppCompatActivity {

private boolean addition=true;

public static final String ADDITION = "";

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

    Button playButton = (Button)findViewById(R.id.playButton);

    playButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(StartActivity.this, Main2Activity.class);
            i.putExtra(ADDITION,addition);
            startActivity(i);
        }
    });

Well, this is better. Fits on one screen and we see the problem directly.

Same thing with the recovering:

public class Main2Activity extends AppCompatActivity {

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

        Intent i = getIntent();
        Bundle b = i.getExtras();
        boolean addition = b.getBoolean("ADDITION");
    }
}

Mmmh, could use StartActivity.ADDITION here ...

AxelH
  • 14,325
  • 2
  • 25
  • 55
1

I hope code will help you Intent intent = new Intent(this, NextActivity.class); intent.putExtra("yourBoolName", true);

 protected void onCreate(Bundle savedInstanceState) {
Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");

}

Rahul Karande
  • 259
  • 2
  • 9
1

Intent intt=new Intent(this,YourAcivity.class);

intt.putExtra("BooleanKey",true);
startActivity(intt);





 protected void onCreate(Bundle savedInstanceState) {
Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");

}

Rahul Karande
  • 259
  • 2
  • 9