How can I send data from one activity (intent) to another?
I use this code to send data:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
Just a suggestion:
Instead of using "id" or "name" in your i.putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent.EXTRA_something.
A full list can be found at Intent (Android Developers).
Getting Different Types of Extra from Intent
To access data from Intent you should know two things.
There are different methods in Intent class to extract different kind of data types. It looks like this
So if you know the datatype of your varibale which you set in otherActivity you can use the respective method.
Example to retrieve String in your Activity from Intent
List of different variants of methods for different dataType
You can see the list of available methods in Official Documentation of Intent.
In the receiving activity
You can get any type of extra datan from intent, no matter if it's an object or sting or anytype data.
Pass the intent with value on First Activity:
Receive intent on second Activity;-
We use generally two method in intent to send the value and to get the value. For sending the value we will use
intent.putExtra("key", Value);
and during receive intent on another activity we will useintent.getStringExtra("key");
to get the intent data asString
or use some other available method to get other types of data (Integer
,Boolean
, etc.). The key may be any keyword to identify the value means that what value you are sharing. Hope it will work for you.