本文轉自:https://www.cnblogs.com/fuck1/p/5456337.html,請點擊鏈接查看原文,尊重樓主版權。
Android的startActivityForResult()與onActivityResult()與setResult()參數分析,activity帶參數的返回
一、使用場景
在一個主界面(主Activity)通過意圖跳轉至多個不同子Activity上去,當子模塊的代碼執行完畢後再次返回主頁面,將子activity中得到的數據顯示在主界面/完成的數據交給主Activity處理。這種帶數據的意圖跳轉需要使用activity的onActivityResult()方法。
(1)startActivityForResult(Intent intent, int requestCode);
第一個參數:一個Intent對象,用於攜帶將跳轉至下一個界面中使用的數據,使用putExtra(A,B)方法,此處存儲的數據類型特別多,基本類型全部支持。
第二個參數:如果> = 0,當Activity結束時requestCode將歸還在onActivityResult()中。以便確定返回的數據是從哪個Activity中返回,用來標識目標activity。
與下面的resultCode功能一致,感覺Android就是爲了保證數據的嚴格一致性特地設置了兩把鎖,來保證數據的發送,目的地的嚴格一致性。
(2)onActivityResult(int requestCode, int resultCode, Intent data)
第一個參數:這個整數requestCode用於與startActivityForResult中的requestCode中值進行比較判斷,是以便確認返回的數據是從哪個Activity返回的。
第二個參數:這整數resultCode是由子Activity通過其setResult()方法返回。適用於多個activity都返回數據時,來標識到底是哪一個activity返回的值。
第三個參數:一個Intent對象,帶有返回的數據。可以通過data.getXxxExtra( );方法來獲取指定數據類型的數據,
(3)setResult(int resultCode, Intent data)
在意圖跳轉的目的地界面調用這個方法把Activity想要返回的數據返回到主Activity,
第一個參數:當Activity結束時resultCode將歸還在onActivityResult()中,一般爲RESULT_CANCELED , RESULT_OK該值默認爲-1。
第二個參數:一個Intent對象,返回給主Activity的數據。在intent對象攜帶了要返回的數據,使用putExtra( )方法。上面由濟南大介紹。
Demo,介紹一下:在主activity裏面讓用戶分別在2個edittext裏面輸入兩個數,然後將這兩個數傳遞至下面的activity,在下面的activity裏面計算結果並返回。
首先是主activity
/*
* 在activity跳轉的時候實現數據的傳遞與返回
*
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button button;
private final static int REQUESTCODE = 1; // 返回的結果碼
private EditText one, two, result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText) findViewById(R.id.Text_one);
two = (EditText) findViewById(R.id.Text_two);
result = (EditText) findViewById(R.id.Text_result);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 獲取用戶輸入的兩個值
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString());
// 意圖實現activity的跳轉
Intent intent = new Intent(MainActivity.this,
OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b);
// 這種啓動方式:startActivity(intent);並不能返回結果
startActivityForResult(intent, REQUESTCODE); //REQUESTCODE--->1
}
});
}
// 爲了獲取結果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// RESULT_OK,判斷另外一個activity已經結束數據輸入功能,Standard activity result:
// operation succeeded. 默認值是-1
if (resultCode == 2) {
if (requestCode == REQUESTCODE) {
int three = data.getIntExtra("three", 0);
//設置結果顯示框的顯示數值
result.setText(String.valueOf(three));
}
}
}
}
與主activity對應的佈局文件:
<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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/Text_one"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" + " />
<EditText
android:id="@+id/Text_two"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = " />
<EditText
android:id="@+id/Text_result"
android:layout_width="80dp"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="計算結果" />
</LinearLayout>
第二個activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OtherActivity extends Activity {
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
button = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.msg);
editText = (EditText) findViewById(R.id.Text_three);
// 去除傳遞過來的意圖,並提取數據
Intent intent = getIntent();此處並不是創建而是直接獲取一個intent對象Return the intent that started this activity.
int a = intent.getIntExtra("a", 0); // 沒有輸入值默認爲0
int b = intent.getIntExtra("b", 0); // 沒有輸入值默認爲0
textView.setText(a + " + " + b + " = " + " ? ");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
// 獲取用戶計算後的結果
int three = Integer.parseInt(editText.getText().toString());
intent.putExtra("three", three); //將計算的值回傳回去
//通過intent對象返回結果,必須要調用一個setResult方法,
//setResult(resultCode, data);第一個參數表示結果返回碼,一般只要大於1就可以,但是
setResult(2, intent);
finish(); //結束當前的activity的生命週期
}
});
}
}
佈局文件
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/Text_three"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回結果" />
</LinearLayout>