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 nullpointerexception.
Please help me solve the problem.
Bundle bundle = new Bundle();
String url = "http://www.google.com";
bundle.putString("url", url);
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(bundle);
context.startService(myIntent);
Get Value code :
if (!getIntent().getExtras().getString("url").contains(null)) {
// Do something
}
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
You use getIntent() to get the Intent that started the second activity. Then you can extract the data with getExtras() and the key you defined in the first activity.
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");
}
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");
if (getIntent().getExtras().getString("url") != null) {
// retrieve the url
}
you have to check against null values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With