Tuesday, June 23, 2015

Passing data from one activity to another

To pass data from one activity to another, the putExtra() method of Intent object can be used.
Here, there are two activities, Activity1 and Activity2.
Activity1 contains an EditText and a Button. On clicking the button, the value entered in the EditText should be passed to Activity2 and the value will be displayed in a TextView.

All the activities in the project needs to be defined in the manifest file. The main activity will be defined in manifest by default. The Activity2 needs to included in the manifest file as follows:

    </activity>
       <activity android:name="Activity2"></activity>
    </application>


To enable button click, the Actvity1 needs to implement the OnClickListener and have to bind the click event to the button.

public class Activity1 extends Activity implements OnClickListener {
     @Override 
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity1);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
     } 
 
     @Override 
     public void onClick(View v)
     {
       // TODO 
     }
} 

We can use the puExtra() method of Intent object to pass data from one Activity1 to Activity2. Data is passed as extras and as key,value pairs. Here the key is 'text1' and is defined as below: 

public static final String text1="text1";

To get the value of text entered in the EditText the getText() method is used. The getText() returns a SpannableString so either while passing the data or while retrieving the data we need to convert it to string else will result in the following exception:
java.lang.ClassCastException: android.text.SpannableString 
cannot be cast to java.lang.String

To pass data on button click, the following code is used. 

   @Override 
   public void onClick(View v)
   {
      Intent i = new Intent(this,Activity2.class);
      EditText textField   = (EditText)findViewById(R.id.editText);
      i.putExtra(text1,textField.getText());
      this.startActivity(i);
   }

Here 'text1' is the key that is passed from Activity1 and it value is the text entered. From Activity2, need to fetch the value of the key and set it to the TextView.

public class Activity2  extends Activity {
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity2);
        String str="";
        Intent intent = getIntent();
        if (null != intent) {
            str = intent.getExtras().get(text1).toString();
        }
        TextView textValue = (TextView) findViewById(R.id.textView);
        textValue.setText(str);
    }


 To make the key available in Acticity2 we need to import it as follows:

   import static com.example.athira.example1.Activity1.text1;
 
To avoid the ClassCastException mentioned above, while retrieving the data the toString() method is used.

No comments:

Post a Comment