Using putExtra() and getExtras() in android

September 3, 2012 | Rishabh Agrawal | General

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity.

putExtra() adds extended data to the intent.

It has two parameters, first one specifies the  name which of the extra data,and the second parameter is the data itself.

getExtra() fetches data which was added using putExtra() in the following way:

Bundle extras= getIntent().getExtras();

Bundle is basically a mapping from String values to various Parcelable types.

getIntent() returns the intent that started this activity.

The below example shows the usage :

Example:

 

/*Intent created from FirstScreen to SecondScreen*/

Intent i = new Intent(FirstScreen.this, SecondScreen.class);

String keyIdentifer  = null;

i.putExtra(“STRING_I_NEED”, strName);//adding additional data using putExtras()

 

Then, to retrieve the value you can use the below mentioned code:

 

Bundle extras;

String newString;

if (savedInstanceState == null)

{

/*fetching extra data passed with intents in a Bundle type variable*/

 

extras = getIntent().getExtras();

if(extras == null)

{

newString= null;

}

else

{

/* fetching the string passed with intent using ‘extras’*/

newString= extras.getString(“STRING_I_NEED”);

}

}


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal