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 = new Intent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);

Take the bundle like this in new Activity inside onCreate

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

Solution 2:

Hi i hope this code helps you.

Bundle bundle = new Bundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);

In MyActivity.class

public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String name = getBundle.getString("name");
String id = 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 = new Intent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);

// get value from bundle like this

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String filter = bundle.getString("filter");

Solution 5:

try this

 String url = "http://www.google.com";

Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras("url", url);
startActivity(myIntent);

and on another activity get it like

Bundle extra = getIntent().getExtras();
if(extra != null) {
String value = 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"