In the previous blog post, we demonstrated the intent to return data by demo, which we continue to demonstrate through a demo, given the importance of the intent to return the data.
In this demo, we enter the user name and password in the other activity, the user clicks the Back button, returns to Mainactivity, the user name and password back, and displays in the Mainactivity TextView.
1. In Activity_main.xml, add TextView and button.
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Fill_parent"android:layout_height="Fill_parent" Android:orientation="vertical" > <TextView android:id= "@+id/tv01"android:layout_width="Fill_ Parent "android:layout_height=" wrap_content "/> <button android:id="@+id/btn01"android:layout_width="Fill _parent "android:layout_height=" Wrap_content "android:text=" Get User name and password "/> </linearlayout>
2. Create a new Other.xml, add two edittext and a button to enter the user name and password.
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:orientation="vertical" > <EditText android:id= "@+id/ed01"android:layout_width="Fill_ Parent "android:layout_height=" Wrap_content "> </EditText> <EditText android:id= "@+id/ed02"android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "> </EditText> <button android:id="@+id/btn01"android:layout_width="fill_parent "android:layout_height=" Wrap_content "android:text="back "> </Button></linearlayout>
3, in Other.java, we get the user name and password entered by the user, and declare an intention to pass the user name and password through the Putextra () method.
PackageCom.example.intentback2;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public class other extends Activity { PrivateEditText ed01;PrivateEditText ed02;PrivateButton Btn01; Public Static FinalString key_user_id ="key_user_id"; Public Static FinalString Key_user_password ="Key_user_password";@Override Public void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.other); ED01 = (EditText) This. Findviewbyid (R.ID.ED01); ED02 = (EditText) This. Findviewbyid (R.ID.ED02); Btn01 = (Button) This. Findviewbyid (R.id.btn01); Btn01.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View arg0) {//TODO auto-generated method stubIntent Intent =NewIntent (); Intent.putextra (key_user_id, Ed01.gettext (). toString ()); Intent.putextra (Key_user_password, Ed02.gettext (). toString ()); Setresult (result_ok,intent); Finish (); } }); }}
4, in Mainactivity, declare intent and through the Getstringextra () method to obtain the user name and password passed from the previous page.
PackageCom.example.intentback2;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public class mainactivity extends actionbaractivity { PrivateTextView tv01;PrivateButton Btn01; Public Final intREQUEST =1;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); TV01 = (TextView) This. Findviewbyid (R.ID.TV01); Btn01 = (Button) This. Findviewbyid (R.id.btn01); Btn01.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View arg0) {//TODO auto-generated method stubIntent Intent =NewIntent (mainactivity. This, Other.class); Startactivityforresult (Intent, REQUEST); } }); }@Override protected void Onactivityresult(intARG0,intArg1, Intent arg2) {//TODO auto-generated method stub /** * Arg0:requestcode arg1:resultcode arg2:data * * Super. Onactivityresult (arg0, arg1, arg2);if(arg0 = = REQUEST && arg1 = = RESULT_OK) {String str ="account number:"+ Arg2.getstringextra (other.key_user_id) +"\ n"+"Password:"+ Arg2.getstringextra (Other.key_user_password); Tv01.settext (str); } toast.maketext (mainactivity. This,"Requestcode ="+ arg0 +"ResultCode ="+ arg1, Toast.length_long). Show (); }}
Note that from the mainactivity we set the request code by defining the Request=1, and then through the Startactivityforresult (intent, request), the method to start the intent, into the other activity. Back when the Android callback Onactivityresult method, we receive the returned value by judging the request and the requested result code (RESULT_OK).
[Android] Intent callback Data 2