Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does intent.putExtra do?

i just started learning android development and a bit confused about what intent.putExtra does. Can somebody please explain this?

Thanks

like image 613
user3276396 Avatar asked Feb 21 '26 23:02

user3276396


2 Answers

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");
 }
like image 124
Shaon Hasan Avatar answered Feb 24 '26 13:02

Shaon Hasan


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.

like image 35
user3109924 Avatar answered Feb 24 '26 11:02

user3109924