有时候我们由一个Activity启动另一个Activity的时候,我们需要从目标Activity中得到处理的信息返回源Activity中,这里就需要用到有返回结果的Activity.
我们源Activity中启动目的Activity要用到startActivityForResult (Intent intent, int requestCode)方法。
其中intent可以包含目标Activity对象或者一些需要处理的信息;requestCode就是对应一个目标Activity的编号,因为一个源Acticity可以启动多个目标Activity.
请看下面代码:
源Acticity:
public class ActivityresultActivity extends Activity { /**发送按钮*/ private Button startButton; /**显示返回结果按钮*/ private TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button) findViewById(R.id.start_result); textView = (TextView) findViewById(R.id.show_result); startButton.setOnClickListener(new OnClickListener() { Intent intent = new Intent(); public void onClick(View v) { intent.setClass(ActivityresultActivity.this, Response.class); /*将intent装载目的地Activity,经过处理后,将在onActivityResult回调函数中得到想要的信息*/ ActivityresultActivity.this.startActivityForResult(intent, 0); } }); } /** * 得到返回的信息,并处理 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == 0) { if(resultCode == RESULT_OK) { /*得到另一个Activity处理后返回的值*/ String getValue = data.getStringExtra("getValue"); System.out.println(getValue); textView.setText(getValue); } } } }
intent.setClass(ActivityresultActivity.this, Response.class);
其中Response是目标Acitity类.
要得到目标Activity:Response处理后的信息,就要重写回调函数onActivityResult()方法,如上,requestCode就是刚才对应的目标Acitivity的编号,这个用来判断是哪个目标Activity返回来的信息。resultCode是返回的状态,在目标Activity中定义,稍后贴上代码。
这个Activity对应的layout:
<?xml version="1.0" encoding="utf-8"?> <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/show_result" android:layout_width="fill_parent" android:layout_height="50px" android:background="@android:color/white" /> <Button android:id="@+id/start_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/start_button" /> </LinearLayout>
目标Activity:Response
public class Response extends Activity { private EditText editText; private Button enterButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.manipulate); System.out.println("ResultActivity onCreate"); editText = (EditText) findViewById(R.id.input_text); enterButton = (Button) findViewById(R.id.enter_finish); enterButton.setOnClickListener(new OnClickListener() { Intent intent = new Intent(); public void onClick(View v) { intent.setClass(Response.this, ActivityresultActivity.class); String getValue = editText.getText().toString(); intent.putExtra("getValue", getValue); Response.this.setResult(RESULT_OK, intent); //当调用finsh()方法时,这个intent就会传递回调用这个Activity的源Activity的onActivityResult()方法里 Response.this.finish(); } }); } }
返回状态就在目标Activity的setResult()方法中设置. 以上代码中有注释关键的finish()作用.
Response对应的layout:
<?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/input_text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/enter_finish" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/enter_button" /> </LinearLayout>
程序运行结果图:
源Activity:白色区域为一个TextView,用来显示从目标Activity中得到的信息
目标Activity:白色区域为EditText,输入的值传递回源Activity中
源Activity:得到返回值,并显示