Sunday 30 October 2016

How do I get extra data from intent on Android?



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);

Answer



First, get the intent which has started your activity using the getIntent() method:



Intent intent = getIntent();



If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:



String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...