onActivityResult
在一個主界面(主Activity)上能連接往許多不同子功能模塊(子Activity),當子模塊的事情做完之後就回到主界面,或許還同時返回一些子模塊完成的數據交給主Activity處理。
startActivityForResult(Intent intent, int requestCode);
使用onActivityResult接收子Activity所回傳的資料,就需要使用這個方法開啟子Activity,使用Intent將需要的資料傳送至子Activity,並傳送requestCode用以在子Activity回傳資料時辨別資料是由哪個Activity返回。
setResult(int resultCode, Intent data)
在子Activity中呼叫這個方法把需要的資料藉由Intent回傳,resultCode一般用來回傳使用上的資訊,通常為RESULT_CANCELED 或是 RESULT_OK。
onActivityResult(int requestCode, int resultCode, Intent data)
藉由requestCode以便確認返回數據的是哪個Activity,resultCode則對應至setResult中傳送的resultCode,Intent對象則用來傳送返回的資料。
範例
首先是畫面排版
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#FF0000"
tools:context="com.example.flowmahuang.doforexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是主Activity"
android:textSize="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainActivityButton"
android:layout_centerInParent="true"
android:text="跳至子Activity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/intText"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#ffffff"/>
</RelativeLayout>
activity_other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:hint="在此輸入數字"
android:inputType="number"
android:id="@+id/otherEditText"/>
<Button
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2"
android:text="按下後回到MainActivity"
android:id="@+id/otherButton"/>
</LinearLayout>
接著是Activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
public Button mainButton;
public TextView intText;
public int resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //設置畫面,這邊使用的是上面編輯好的Xml檔案的布局設定
resultText = 1;
intText = (TextView) findViewById(R.id.intText); //使用findViewById藉由ID抓取在Xml檔中的元件TextView
intText.setText(resultText + ""); //抓取到元件後設置其文字為要顯示出來的數字
mainButton = (Button) findViewById(R.id.mainActivityButton); //使用findViewById藉由ID抓取在Xml檔中的元件Button
mainButton.setOnClickListener(new View.OnClickListener() { //設置按鈕按下時觸發的事件
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("fromMain", resultText);
startActivityForResult(intent, 0);
//使用startActivityForResult藉由intent傳送資料給OtherActivity,並設定requestCode使資料回傳時可以判斷資料來自何處
}
});
}
//使用onActivityResult接收其他Activity回傳的資料
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) { //使用if判斷requestCode是由OtherActivity傳回且resultCode為RESULT_OK
resultText = data.getIntExtra("fromOther", 0); //接收在其他Activity計算完的資料
intText.setText(resultText + "");
}
}
}
OtherActivity.java
public class OtherActivity extends AppCompatActivity {
public EditText ed;
public Button otherButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other); //設置畫面,這邊使用的是上面編輯好的Xml檔案的布局設定
ed = (EditText) findViewById(R.id.otherEditText); //使用findViewById藉由ID抓取元件
otherButton = (Button) findViewById(R.id.otherButton);
otherButton.setOnClickListener(new View.OnClickListener() { //設置按鈕按下時觸發的事件
@Override
public void onClick(View v) {
if (ed.getText().toString().equals("")) {
Toast.makeText(OtherActivity.this, "請輸入數字到輸入框", Toast.LENGTH_SHORT).show();
//設定如果EditText沒有輸入任何數字即會跳出警告訊息
}
else {
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
Intent mainIntent = getIntent();
int caculated = Integer.parseInt(ed.getText().toString()); //取得EditText中輸入的數字
int caculate = mainIntent.getIntExtra("fromMain", 0); //接收來自MainActivity的資料
caculated = caculate + caculated;
intent.putExtra("fromOther", caculated);
setResult(RESULT_OK, intent); //設置要回傳MainActivity的資料,並設置requestCode給予訊息RESULT_OK
finish(); //結束這個Activity
}
}
});
}
}