DocDevelopers
DocDevelopers

Reputation: 33

How to transfer boolean value between activities in android?

This is how I am currently doing it but, it just force closes app.

In the first activity

Intent myIntent = new Intent(Input.this, results.class);
    myIntent.putExtra("perfect", rig);
    startActivity(myIntent);`

Activity I want to transfer to

    Boolean lovers = getIntent().getExtras().getBoolean("perfect");

Upvotes: 3

Views: 9329

Answers (3)

Maddy Sharma
Maddy Sharma

Reputation: 4956

You can try this at sending side:

MyModel model = new MyModel();

 //1. using constructor
    Boolean blnObj1 = new Boolean(model.getBooleanStatus()); // This //getBooleanStatus will return 'boolean' value

    //2. using valueOf method of Boolean class. This is a static method.
    Boolean blnObj2 = Boolean.valueOf(model.getBooleanStatus());
  }

Intent targetIntent = new Intent(MyCalass.this, TargetClass.class);
targetIntent.putExtra("STATUS", new Boolean(model.getBooleanStatus()));
targetIntent.putExtra("STATUS", Boolean.valueOf(model.getBooleanStatus()));
startActivity(targetIntent);

Receiving side :

Intent receiverIntent = getIntent().getBoolean("STATUS");

Upvotes: 1

Devrath
Devrath

Reputation: 42854

I am not sure about the accepted answer:: But i think it should be

Boolean lovers = getIntent().getExtras().getBoolean("perfect");

Upvotes: 3

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

As the docs say, the function is getBooleanExtra

Boolean lovers = getIntent().getExtras().getBooleanExtra("perfect");

Upvotes: 5

Related Questions