i just started learning android development and a bit confused about what intent.putExtra does. Can somebody please explain this?
Thanks
An Android application can contain zero or more activities. When your application has more than one activity, you often need to navigate from one to another. In Android, you navigate between activities through what is known as an intent. By using putExtra(), you can pass some information to the activity you are intending to start. eg:
//code inside the recent activity, from where you want to start another activity
Intent myIntent = new Intent(this, SecondActivity.class);
myIntent.putExtar("name","xx");
myIntent.putExtra("age",30);
startActivity(myIntent);
// code inside another activity(SecondActivity), which you are starting using intent
Bundle resultIntent = getIntent().getExtras();
if(resultIntent != null)
{
String nameValue = resultIntent.getString("name");
int ageValue = resultIntent.getInt("age");
}
When you start a new Intent, you may want to pass some information to it. putExtra is how you so that. For instance if you made an intent to display account details, you might pass it the account id that is to be displayed. Basically any information you put in the extra bundle can be read later by the intent you've given it to.
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