Hrnsvnc Blog

Software, Technology and stuff


How to Transfer Data from one Activity to another in Android

Today i will show you how to properly transfer data from one activity to another.

The simplest way to transfer simple data like Strings or numeric data is by using a bundle. A bundle is a data-structure like a Map in Java. You can save data in key value pairs in it.
The code:

//First create a Bundle
 Bundle bundle = new Bundle();

//Now put some data into the Bundle

Thats it. You now can put the Bundle into an intent and call it from your next activity.

//Create an intent
 Intent intent = new Intent(this,ActivityNameYouWantNext.class);
 //Now put your Bundle into your intent;
 //Next call an Activity with your intent

In your receiving activity you cam access your data by calling “getIntent()” and then you can call “getExtras()”:

 Intent intent = getIntent();
 final Bundle bundle = intent.getExtras();
//Save your data inside a variable
 String myData = bundle.getString(KeyName);

And there it is: Your data inside another activity.

Please follow and like us:
Tweet 20

Leave a Reply

Your email address will not be published. Required fields are marked *