Android: returns data from another activity.
First, let's take a look at the structure of the data returned by the activity.
Previously we started another activity using
startActivity(Intent intent)
If you want to open another activity and return results, you must use
startActivityForResult(Intent intent,int requestCode)
Intent does not need to be mentioned. requestCode is what the Request Code should do to distinguish each request code in another activity.
The specific functions are as follows:
Our implementation result is:
The main interface is like this. When we click the select button, another activity is opened. When we click the number, the activity is ended and the number is displayed in the edit box of the main activity.
The specific implementation code is as follows.
MainActivity
Package com. example. activityforresult; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {private EditText editText; private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); editText = (EditText) findViewById (R. id. editText); button = (Button) findViewById (R. id. button); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // startActivity () cannot be used to obtain the result from another activity () method to use this method startActivityForResult (new Intent (MainActivity. this, ResultActivity. class), 0x1) ;}}) ;}/ ** if you want to get the returned result, you must implement this class **/@ Overrideprotected void onActivityResult (int requestCode, int resultCode, intent data) {// TODO Auto-generated method stub // if data is null, return if (data = null) return; String phoneNumber = data. getExtras (). getString ("phoneNumber"); // the content in the update edit box is the editText of the selected number. setText (phoneNumber );}}
SetResult (int resultCode, Intent data) is required in ResultActivity. The selected number is returned. ResultCode is an int type, while data is an Intent type. Just create an intent object.
Call the finish () method to end the activity. Do not forget to configure the following information in AndroidManifest:
The ResultActivity code is as follows:
package com.example.activityforresult;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class ResultActivity extends Activity{private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_result);textView=(TextView) findViewById(R.id.textView);textView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent();intent.putExtra("phoneNumber", textView.getText().toString());setResult(0x1, intent);finish();}});}}
Activity_main.xml
Activity_result.xml
This is the running result