Hafeez Lawal Olabisi February 2016

Passing data from two different intent in one activity to another activity

how can i pass different value from different intent inside another activity. i.e

Activity A:

ButtonA .onclick{

    intent.putExtra("name", screenname);

    intent.putExtra("email", description);

    intent.putExtra("pic", twitterImage);

    startActivity(intent);

}

ButtonB. onClick{

    intent.putExtra("anothervalue", json_object.toString())

}

Activity B:

  Intent intent = getIntent();

  String getValue = intent.getStringExtra("value from any of the button clicked")

Answers


David Rauca February 2016

String getValue = intent.getStringExtra("firstvalue")  // in order to get the first value that was set when user clicked on buttonA

or

String getValue = intent.getStringExtra("anothervalue")  // in order to get the the value that was set when user clicked on buttonB


Rohit5k2 February 2016

Activity B code should be like this

Intent intent = getIntent();
String firstvalue = intent.getStringExtra("firstvalue");
String anothervalue = intent.getStringExtra("anothervalue");

if(firstvalue != null)
    // called from Button A click
else if(secondvalue != null)
    // called from Button B click


Hein February 2016

While David Rauca answer is essentially correct, you will likely face NullPointerException.

getIntent().getStringExtra("firstvalue") will cause NPE if there is no value with name 'firstvalue'.

You should check whether or not value exist like this.

if(getIntent().hasExtra("firstvalue")) {
    String firstvalue = getIntent().getStringExtra("firstvalue");
}


Mandeep February 2016

Intent intent = getIntent();
String getValue = null;

if(intent.hasExtra("firstvalue")){ 

getValue = intent.getStringExtra("firstvalue");

}

if(intent.hasExtra("anothervalue")){

 getValue = intent.getStringExtra("anothervalue");

}

Post Status

Asked in February 2016
Viewed 2,547 times
Voted 14
Answered 4 times

Search




Leave an answer