Returns data from the Activity. The Activity returns data.
Return data from Activity
I. Introduction
In this example, the intent method is used to return data.
Ii. Steps
Access the Activity01 page through a button in MainActivity, and then return the data on the Activity01 page to the MainActivity page.
Call Activity page: MainActivity
Called page: Activity01
Receiving value page: MainActivity
Value Transfer page: Activity01
1,Change the startActivity method on the page to startActivityForResult.To receive data.
StartActivityForResult (intent, 0 );
2. Use intent. putExtra to pass the value on the value passing page, and then close the page.
Intent intent = new Intent ();
Intent. putExtra ("phoneNumber", TV _phoneNum.getText ());
SetResult (0, intent );
Finish ();
3. Rewrite the onActivityResult method on the receiving value page to receive data.
Bundle bundle = data. getExtras ();
String phoneNumber = bundle. getString ("phoneNumber ");
Et_phoneNumber.setText (phoneNumber );
Note:
Activity01 call the onActivityResult method of MainActivity after the finish () method is destroyed,
Let's look at the instructions on the finish method. In the eclipse code, click the finish () method.
Iii. code example
Result chart:
Code:
ActivityReturnData. MainActivity
1 package activityReturnData; 2 3 4 5 6 import com. example. activityReturnData. r; 7 8 import android. app. activity; 9 import android. content. intent; 10 import android. OS. bundle; 11 import android. view. view; 12 import android. view. view. onClickListener; 13 import android. widget. button; 14 import android. widget. editText; 15 16 17 18 public class MainActivity extends Activity {19 private Button btn_chooseContacter; // create a button object 20 private EditText et_phoneNumber; 21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); // parent class operation 23 setContentView (R. layout. activity_main); // introduce interface 24 btn_chooseContacter = (Button) findViewById (R. id. btn_chooseContacter); // find the button25 et_phoneNumber = (EditText) findViewById (R. id. et_phoneNum); 26 btn_chooseContacter.setOnClickListener (new OnClickListener () {// set the button to listen to 27 28 @ Override29 public void onClick (View v) {// onclick event 30 // TODO Auto-generated method stub31 Intent intent = new Intent (MainActivity. this, Activity01.class); // initialize intent32 // Request Code: resultCode33 startActivityForResult (intent, 0); 34} 35 }); 36} 37/* 38 * Activity01 after the finish () method is called, the onActivityResult method of MainActivity will be called 39 * because after Activity01 is completed, the method of calling it will be returned 40 * (non-Javadoc) 41 * @ see android. app. activity # onActivityResult (int, int, android. content. intent) 42 */43 @ Override44 protected voidOnActivityResult(Int requestCode, int resultCode, Intent data) {45 // TODO Auto-generated method stub46 super. onActivityResult (requestCode, resultCode, data); 47 // if no value is returned, return is OK 48 if (data = null) return; 49 Bundle bundle = data. getExtras (); 50 String phoneNumber = bundle. getString ("phoneNumber"); 51 et_phoneNumber.setText (phoneNumber); 52} 53}
ActivityReturnData. Activity01
1 package activityReturnData; 2 3 4 import com. example. activityReturnData. r; 5 6 import android. app. activity; 7 import android. content. intent; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. textView; 12 13 public class Activity01 extends Activity {14 private TextView TV _phoneNum; 15 @ Override16 protected void onCreate (Bundle savedInstanceState) {17 // TODO Auto-generated method stub18 super. onCreate (savedInstanceState); 19 setContentView (R. layout. activity01); 20 TV _phoneNum = (TextView) findViewById (R. id. TV _phoneNum); 21 TV _phoneNum.setOnClickListener (new OnClickListener () {22 23 @ Override24 public void onClick (View v) {25 // TODO Auto-generated method stub26 Intent intent = new Intent (); 27Intent. putExtra ("phoneNumber", TV _phoneNum.getText ());28 // resultCode, result code 29SetResult (0, intent );30 finish (); 31} 32}); 33} 34}
/ActivityReturnData/AndroidManifest. xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activityReturnData" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application11 android:allowBackup="true"12 android:icon="@drawable/ic_launcher"13 android:label="@string/app_name"14 android:theme="@style/AppTheme" >15 <activity16 android:name="activityReturnData.MainActivity"17 android:label="@string/app_name" >18 <intent-filter>19 <action android:name="android.intent.action.MAIN" />20 21 <category android:name="android.intent.category.LAUNCHER" />22 </intent-filter>23 </activity>24 <activity android:name="activityReturnData.Activity01" android:exported="true"></activity>25 </application>26 27 </manifest>