user3041073
user3041073

Reputation: 33

Android Intent won't pass value from Switch

I have a Switch button, from which I want to pass different values (1 or 2) to next activity for calculations. But I always get 0. Any help?

 Switch mySwitch = (Switch) findViewById(R.id.edit_home);

     // set the switch to ON
     mySwitch.setChecked(true);
     // attach a listener to check for changes in state
     mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

     if (isChecked) {

     double input5 = 2;  
     Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
     intentx.putExtra("IFValue",input5);
             startActivity(intentx);
     Toast.makeText(getApplicationContext(), "Home field is important advantage",
     Toast.LENGTH_SHORT).show();

     } else {

     double input5 = 1;
     Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
     intenty.putExtra("IFValue",input5);
             startActivity(intentx);
     Toast.makeText(getApplicationContext(),
     "Home field is not advantage", Toast.LENGTH_SHORT).show();
     }

     }
     });

In next activity:

     Bundle bundle = getIntent().getExtras();
     double input5 = bundle.getDouble("IFValue");

EDITED: I want to send values (input5) to the next activity, but if I add startActivity(intentx) inside of switch; then goes there imediately and not when I press next Button (buttonForward) as I want to.

Rest of the code:

    Switch mySwitch = (Switch) findViewById(R.id.edit_home);

     // set the switch to ON
     mySwitch.setChecked(true);
     // attach a listener to check for changes in state
     mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

     if (isChecked == true) {

     double input5 = 2;  
     Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
     intentx.putExtra("IFValue",input5);
     startActivity(intentx);
     Toast.makeText(getApplicationContext(), "Home field is important advantage",
     Toast.LENGTH_SHORT).show();

     } else {

     double input5 = 1;
     Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
     intenty.putExtra("IFValue",input5);
     startActivity(intenty);
     Toast.makeText(getApplicationContext(),
     "Home field is not advantage", Toast.LENGTH_SHORT).show();
     }

     }
     });



    Button buttonForward = (Button) findViewById(R.id.buttonToMain2);
    buttonForward.setOnClickListener(new OnClickListener(){


        @Override
        public void onClick(View arg0) {

    EditText edit_team = (EditText) findViewById(R.id.edit_team);
    EditText edit_form = (EditText) findViewById(R.id.edit_form);

    EditText edit_import = (EditText) findViewById(R.id.edit_import);

    String ekipa1 = edit_team.getText().toString();

    final double input2 = Double.valueOf(qualityControl.getProgress());
    final double input3 = Double.valueOf(edit_form.getText().toString());
        final double input4 = Double.valueOf(ratingBar.getRating()); 

    final double input6 = Double.valueOf(edit_import.getText().toString());

    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",input2);
    intent.putExtra("Value1",input3);
    intent.putExtra("Value2",input4);

    intent.putExtra("Value4",input6);
    intent.putExtra("team1", ekipa1);
    startActivity(intent);








        }



    });     

Upvotes: 1

Views: 356

Answers (1)

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

You are creating a local instance of the Intent (intentx & intenty) inside the listener (I do not see you calling startActivity() inside the listener). The passed Extras only apply to the local copy not to the one you are using outside the listener with startActivity().

Update 1

Character case matters when using Extra keys:

You are using intentx.putExtra("IFvalue",input5);

while in the next Activity you are using double input5 = bundle.getDouble("IFValue"); which is a different key.

Update 2

I want to send values (input5) to the next activity, but if I add startActivity(intentx) inside of switch; then goes there imediately and not when I press next Button (buttonForward) as I want to.

Then, you will need to have only one Intent instance in the whole Activity (define it before onCreate() and then do not start the Activity from the switch listener, but add the Extra for the single Intent instance:

Intent intent = new Intent(MainActivity.this,Main2Activity.class); // Class level variable

then change the following in the switch listener:

 // Intent intentx=new Intent(MainActivity.this,Main2Activity.class); remove this
 intent.putExtra("IFValue",input5);

and in the buttonForward click listener:

//Intent intent=new Intent(MainActivity.this,Main2Activity.class); remove this
intent.putExtra("Value",input2);
intent.putExtra("Value1",input3);
intent.putExtra("Value2",input4);

intent.putExtra("Value4",input6);
intent.putExtra("team1", ekipa1);
startActivity(intent);

Upvotes: 2

Related Questions