Android应用开发学习笔记之startActivityForResult
如果我们有这样的需求:使用Activity1启动Activity2,Activity2处理完数据后,将处理结果返回给Activity1,这时,可以使用startActivityForResult()函数。下面我们通过一个例子来看这个函数的用法,
我们先来看主布局文件main.xml,其内容如下:
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入第一个数字:" /> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" />" <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入第二个数字:" /> <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:text="提交" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textResult" android:textSize="20dp" android:layout_width="fill_parent" android:layout_height="fill_parent" /> " </LinearLayout>
下面看主Activity,其内容如下:
package com.liuhaoyu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { String num1, num2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText edit1 = (EditText)findViewById(R.id.editText1); final EditText edit2 = (EditText)findViewById(R.id.editText2); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub num1 = edit1.getText().toString(); num2 = edit2.getText().toString(); Intent intent=new Intent(MainActivity.this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putCharSequence("number1", num1); bundle.putCharSequence("number2", num2); intent.putExtras(bundle); startActivityForResult(intent, 0x11); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(0x11 == resultCode) { TextView textResult = (TextView)findViewById(R.id.textResult); String result = data.getStringExtra("result"); textResult.setText(num1 + " + " + num2 + " = " + result); } } }
在启动SecondActivity时,与上篇文章中使用startActivity()函数不同,这里使用的是startActivityForResult(),该函数要求被启动的活动即SecondActivity在处理完数据后,返回一个结果给MainActivity。startActivityForResult函数的第二个参数是两个Activity约定的请求码,可以自己定义,本程序使用0x11。
在MainActivity中,还需要实现onActivityResult回调函数,当SecondActivity将处理结果返回后,会执行这个函数。
SecondActivity的实现代码如下:
package com.liuhaoyu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends Activity { int sum; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); final String num1 = bundle.getString("number1"); final String num2 = bundle.getString("number2"); sum = Integer.parseInt(num1) + Integer.parseInt(num2); TextView text = (TextView)findViewById(R.id.textView); text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum)); Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final String result = String.valueOf(sum); Intent data = new Intent(); data.putExtra("result", result); setResult(0x11, data); finish(); } }); } }
得到处理结果后,当用户点击“返回”按钮,在该按钮的监听函数中,通过调用setResult()函数将要传递给MainActivity的数据返回。
SecondActivity的布局文件内容如下:
<?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" > <TextView android:id="@+id/textView" android:textSize="20dp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:text="返回" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" />" </LinearLayout>
最后,在AndroidManifest.xml文件中声明SecondActivity:
<activity android:icon="@drawable/ic_launcher" android:name=".SecondActivity" android:label="计算结果" android:theme="@android:style/Theme.Dialog" > </activity>
补充:移动开发 , Android ,