How to use Intent with Extra data in 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.

19-04-2022 |

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?


New C# Learning Area - Free!

Our website now offers a new section on C# theory, including code examples and related video tutorials.

ZetBit

Subscribe to ZetBits Newsletter