Monday 19 October 2015

Passing data from one activity to other in android

Passing data from one activity to other in android

 

  • The data can be passed to other activity using intent putExtra() method. 

  • Data is passed as extras and are key/value pairs. 

  • The key is always a String. As value you can use the primitive data types int, float, chars, etc. 

  • We can also pass Parceable and Serializable objects from one activity to other.

*We have two ways for sending data .

Example 1: 

Intent intent=new Intent(MainActivity.this,Main2Activity.class)

*intent.putExtras("key","value");

intent.putExtras("key",Welcome Android");

startActivity(intent); 

Example 2: 

EditText ed1;

Intent intent=new Intent(MainActivity.this,Main2Activity.class)

*intent.putExtras("key","value");

intent.putExtras("key",ed1.getText.toString");

startActivity(intent); 

 

Retrieving data from android activity

 

  • You can retrieve the information using  getData() methods on the Intent object. 

  • The Intent object can be retrieved via the getIntent() method.

Example:

TextView tv;

Intent intent=getIntent();

String s=intent.getStringExtras("key");

tv.setText(s);

 

 

 

 

 

 

 

0 comments:

Post a Comment