這個demo主要示範了如何從其它的activity中傳回預期的結果。要實作這個功能,主要有三個步驟需要注意。
1.在主activity啟動子activity時使用startActivityForResult()方法,而不是使用startActivity()方法。
2.使用了startActivityForResult()方法就必須重寫onActivityResult(int requestCode, int resultCode, Intent data) 并在這個中實作對傳回的結果的處理。
3.在子activity中使用setResult()方法設定傳回的結果。
主布局檔案activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Press the button to get an activity result which will be displayed here:" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:background="#ff00ff88" />
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Get Result"/>
</LinearLayout>
子布局檔案send_result.xml
<?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:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Pick a result to send or BACK to cancel"/>
<Button
android:id="@+id/bt_corky"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="Corky"/>
<Button
android:id="@+id/bt_violet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="Violet"/>
</LinearLayout>
MainActivity類
public class MainActivity extends Activity {
private static final int GET_CODE = ;
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_result=(TextView) findViewById(R.id.tv_result);
//設定textView的顯示模式為允許在原有文本的基礎上追加新的文本
tv_result.setText(tv_result.getText(), BufferType.EDITABLE);
}
public void click(View v){
//顯示聲明意圖
Intent intent=new Intent(this, SendResult.class);
//啟動activity并要求傳回結果,GET_CODE為請求碼
startActivityForResult(intent, GET_CODE);
}
//使用startActivityForResult擷取傳回的結果,就必須重寫onActivityResult方法
//在該方法中對傳回值進行處理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
* 首先對請求碼進行判斷,當需要啟動多個activity,并且需要擷取每個activity傳回的結果,
* 此時就需要用請求碼來區分不同的子activity傳回的結果。
* 本例隻開啟了一個子activity,請求碼的作用不是很明顯
*/
if(requestCode==GET_CODE){
/*
* Editable 是一個接口類型,對它的執行個體化對象作出任何改變都是對原有的執行個體化對象操作的,
* 記憶體位址還是原來的那個。簡單地說就是可以靈活地、動态地追加文本内容。
* 本例就是說隻有一個textView的文本對象,我們對文本所做的修改都是對這個對象所做的修改
* 修改内容會直接展現在這個文本對象上,而不需要再做對textView的指派操作
*/
Editable text=(Editable) tv_result.getText();
/*判斷結果碼,結果碼通過有兩種RESULT_CANCELED和RESULT_Ok,前者表示子activity
* 被取消,沒有傳回預的結果,後者表示傳回預期的結果
* */
if(resultCode==RESULT_CANCELED){
text.append("(Cancelled)");
}else{
text.append("(Okay");
text.append(Integer.toString(resultCode));
text.append(")");
if(data!=null){
text.append(data.getAction());
}
}
text.append("\n");
}
}
}
子activity類SendResult
public class SendResult extends Activity implements OnClickListener {
private Button bt_corky, bt_violet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_result);
bt_corky = (Button) findViewById(R.id.bt_corky);
bt_violet = (Button) findViewById(R.id.bt_violet);
bt_corky.setOnClickListener(this);
bt_violet.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_corky:
sendResultCorky();
break;
case R.id.bt_violet:
sendResultViolet();
break;
default:
break;
}
}
/**
* 設定傳回的結果并finish掉自己
*/
private void sendResultViolet() {
setResult(RESULT_OK, new Intent().setAction("Violet"));
finish();
}
private void sendResultCorky() {
setResult(RESULT_OK, new Intent().setAction("Corky"));
finish();
}
}
别忘了在配置檔案中配置子activity。