White search icon
News
How to use Intent with Extra data in Android Studio
Android Studio

Using Intent in Android Studio With Extra Data

Original YouTube Logo

Article have related YouTube video

For most Android apps it is essential to be able to link between different activities like the anchor tag in HTML. Sometimes you also want to send data from one activity to another. It could be an id or a name.

|

Android Studio

To link between activities is fortunately simple and straight forward.


Simple Intent


Intent intent = new Intent(MainActivity.this, SecondActivity.class);,
startActivity(intent);


Intent with Extra data


Intent intent = new Intent(MainActivity.this, SecondActivity.class);,
intent.putExtra("extra","This is the extra data");
startActivity(intent);


Recieving the data in SecondActivity


String extraText;
//Check if intent has extra
if (intent.hasExtra("extra")) {
    extra = intent.getStringExtra("extra");
} else {
    extra = "No extra data";
}


Like what you see?

Buy me a coffee

ZetBit on Spotify

An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.