Return Data Using Intent Object In Android Applications

Overview
 
In my previous article Intent in Android, we saw how to use Intent in Android Applications. It is very important to get and pass the data between the two activities.
 

Introduction

 
In this article, I will explain about returning and passing the data, using an Intent object in Android Applications.
 
Coding
 
In the same project, add the following statements in the activity_my_second.xml file.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"  
  3. android:layout_height="fill_parent"  
  4. android:paddingLeft="@dimen/activity_horizontal_margin"  
  5. android:paddingRight="@dimen/activity_horizontal_margin"  
  6. android:paddingTop="@dimen/activity_vertical_margin"  
  7. android:paddingBottom="@dimen/activity_vertical_margin"  
  8. android:orientation="vertical"  
  9. tools:context="com.example.administrator.intentexampleapp.MySecondActivity">  
  10.   
  11. <TextView android:text="@string/MySecond_Activity"  
  12. android:layout_width="fill_parent"  
  13. android:layout_height="wrap_content" />  
  14.   
  15. <TextView  
  16. android:layout_width="fill_parent"  
  17. android:layout_height="wrap_content"  
  18. android:layout_marginTop="10dp"  
  19. android:text="Enter your name" />  
  20. <EditText  
  21. android:layout_width="fill_parent"  
  22. android:layout_height="wrap_content"  
  23. android:id="@+id/txtusername" />  
  24. <Button android:layout_height="wrap_content"  
  25. android:layout_width="fill_parent"  
  26. android:text="OK"  
  27. android:id="@+id/btnOk"  
  28. android:onClick="onClick" />  
  29. </LinearLayout>  
The code given below needs to be added in MySecondActivity.java file. The method called onClick is implemented in this code.
  1. public void onClick(View view)  
  2.  {  
  3.      //create an instance of an Intent object.  
  4.      Intent data= new Intent();  
  5.   
  6.      //Get the EditText view and typecast here.  
  7.      EditText txtusername=(EditText)findViewById(R.id.txtusername);  
  8.   
  9.      //set the value/data to pass back  
  10.      data.setData(Uri.parse(txtusername.getText().toString()));  
  11.   
  12.      //set a result code, It is either RESULT_OK or RESULT_CANCELLED  
  13.      setResult(RESULT_OK,data);  
  14.      //Close the activity  
  15.      finish();  
  16.  }  
Reference of the Intent and EditText are also added by pressing <Alt + Enter> key
  1. import android.content.Intent;  
  2. import android.widget.EditText;  
Add the code in the MainActivity.java file, as given below:
  1. public void showSecondActivity(View view)  
  2.      {  
  3.          Intent intent= new Intent(this,MySecondActivity.class);  
  4.  //        startActivity(intent);  
  5.    
  6.          startActivityForResult(intent, request_Value);  
  7.      }  
  8.      public void onActivityResult(int requestCode, int resultCode, Intent data){  
  9.          if(requestCode == request_Value){  
  10.              if(resultCode == RESULT_OK){  
  11.                  Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();  
  12.              }  
  13.          }  
  14.      }  
Explanation
 
The method onClick() is implemented in the MySecondActivity.java file. In this method, an Intent object is used to send the data to the calling activity via setData() method. The setResult() method sets a result code to either RESULT_OK or RESULT_CANCELLED and the data to be returned back to the calling activity (MainActivity.java file). The finish() method is used to close the activity and returns back to the calling activity.
 
In the code, mentioned above, of MainActivity.java file (calling activity), the onActivityResult() method is implemented because it is called whenever an activity returns.
 
Run the Application and get the result, as shown below,
 
 
When the user clicks the button, the second activity is displayed, as shown below,
 
  
Test data is filled by the user and while clicking on the button, entered value displays on the first activity.
 
  

Conclusion

 
In the article, written above, we saw how to return the results/data from an Intent. In the next article, I will explain about passing the data, using an Intent object.


Similar Articles