Home Ask Login Register

Developers Planet

Your answer is one click away!

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);



ButtonB. onClick{

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


Activity B:

  Intent intent = getIntent();

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


David Rauca February 2016

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


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;


getValue = intent.getStringExtra("firstvalue");



 getValue = intent.getStringExtra("anothervalue");


Post Status

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


Leave an answer

Quote of the day: live life