Eduardo Sanchez
Eduardo Sanchez

Reputation: 15

Sending a boolean between activities

Like the title says, i'm trying to send a boolean from one activity to other.

The sending activity is this.

public class ReadyActivity extends Activity {
private TextView text;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ready);
    text = (TextView) findViewById(R.id.contador);
    new CountDownTimer(3000, 1000) {

        public void onTick(long millisUntilFinished) {
            text.setText(""+millisUntilFinished / 1000);
        }

        public void onFinish() {
            Intent intent = new Intent(ReadyActivity.this, StreamingActivity.class);
            intent.putExtra("ready",true);
            startActivity(intent);
            finish();
        }

    }.start();
}

And in the receiving activity I have this just for checking the value

Bundle extras = getIntent().getExtras();
    if(extras != null){
        if(extras.getBoolean("ready")){ // if an extra has been set
            Toast.makeText(getApplicationContext(), "Test 1 Worked", Toast.LENGTH_LONG).show();
        }
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Doesn't work", Toast.LENGTH_LONG).show();
    }

And it always sends me the Doesn't work message.

I have to add that the sending activity has the Theme.Dialog. Does it affect it? How can I send and receive the boolean value and get the value?

Upvotes: 1

Views: 123

Answers (2)

Vinodh
Vinodh

Reputation: 1129

Can you try implementing onNewIntent like below :

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //get intent extras here
}

Upvotes: 0

jianfeng
jianfeng

Reputation: 26

I tried your code on my phone .It was worked. Maybe it's your activity's launchMode cause. Check your xml and set your activity default.Try again.

Upvotes: 1

Related Questions