Passing data between activities in Android

Passing data between activities in Android

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();

b.putInt("key", 1);



There are 2 method which are interesting.
First method is
b.putInt("value", 1);
This method puts in Bundle key “key” and its value 1Bundle class has a lof of put* methods.
Second method is:
This method puts our bundle in Intent which starts new activity.
Now need to get passed data on new SecondActivity.
This process is also simple
Bundle b = getIntent().getExtras();

int value = b.getInt("key", 0);
First method gets needed Bundle.
Second method gets our data. As you can see there is second parameter “0″. It is default value. In situation when Bundle doesn’t contains data you will receive “0″ (default value).
I hope it was useful for you.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: