Quantcast
Channel: Another Android Blog » Android Development
Viewing all articles
Browse latest Browse all 10

Passing Data With an Intent

$
0
0

This is the code to pass data from one activity to another through an Intent.

This is the code used to create and pass data into an Intent.

String dataToPass = "The next activity needs this sentence.";
...
Intent intent = new Intent(this, NextActivity.class);
intent.putExtras("KeyToAccessData", dataToPass);
startActivity(intent);

And this is how you catch that data from the receiving activity:

String dataToCollect;
...
Intent intent = getIntent();
dataToCollect = intent.getStringExtra("KeyToAccessData");

Note that the getIntent() method is part of the Android Activity class. Every Activity knows it’s own intent. We are just creating a way to reference it easily. You can also collect Integers (getIntExtra()) and other various data types using this method.


This post is part of a new snippet series I will be adding to my blog. Basically, it’s a way for me to quickly look up code when my brain decides to bury it. If I have to look it up more than once, I’ll try to add it here as well.

Viewing all articles
Browse latest Browse all 10

Trending Articles