Skip to content Skip to sidebar Skip to footer

Passing Values Through Bundle And Get Its Value On Another Activity

I am passing value through Bundle as you can see in my code. Now, I want its value in another activity onCreate(). I tried it to get its value but it is showing nullpointerexcepti

Solution 1:

This should be the procedure.

Create a new Intent with bundle and start the activity.

Intent i = newIntent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);

Take the bundle like this in new Activity inside onCreate

Bundleextras= getIntent().getExtras();
String value;
if (extras != null) {
  value = extras.getString("key");
}

Solution 2:

Hi i hope this code helps you.

Bundlebundle=newBundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intentintent=newIntent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);

In MyActivity.class

publicBundlegetBundle=null;
getBundle = this.getIntent().getExtras();
Stringname= getBundle.getString("name");
Stringid= getBundle.getString("iname");

Solution 3:

if (getIntent().getExtras().getString("url") != null) {
        // retrieve the url
}

you have to check against null values

Solution 4:

//put value in intent like this

    Intent in = newIntent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);

// get value from bundle like this

Intentintent= getIntent();
    Bundlebundle= intent.getExtras();
    Stringfilter= bundle.getString("filter");

Solution 5:

try this

Stringurl="http://www.google.com";

IntentmyIntent=newIntent(context, NotificationService.class);
myIntent.putExtras("url", url);
startActivity(myIntent);

and on another activity get it like

Bundleextra= getIntent().getExtras();
if(extra != null) {
Stringvalue= extra.getString("url")
//use this value where ever you want

  }

Post a Comment for "Passing Values Through Bundle And Get Its Value On Another Activity"