Android利用startActivityForResult返回?cái)?shù)據(jù)到前一個(gè)Activity
在Android里面,從一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity、再返回,前一個(gè)Activity默認(rèn)是能夠保存數(shù)據(jù)和狀態(tài)的。但這次我想通過利用startActivityForResult達(dá)到相同的目的,雖然看起來變復(fù)雜了,但可以探索下startActivityForResult背后的原理和使用注意事項(xiàng)。
要實(shí)現(xiàn)的功能如下:
從Activity A將數(shù)據(jù)傳到Activity B,再從Activity B中獲取數(shù)據(jù)后,再傳回Activity A。在Activity B中添加一個(gè)“回到上一頁”的Button,返回到Activity A之后,需要保留之前輸入的相關(guān)信息,我們用startActivityForResult來拉起Activity B,這樣,Activity A就會有一個(gè)等待Activity B的返回。
具體步驟如下:
- 在Activity A中有一個(gè)Button,點(diǎn)擊Button后,獲取要傳到Activity B的數(shù)據(jù),將數(shù)據(jù)封裝到Bundle中,再調(diào)用startActivityForResult將數(shù)據(jù)傳到Activity B
- Activity A 重寫onActivityResult函數(shù),判斷requestCode和resultCode是否是我們預(yù)期的結(jié)果,如果是,那么從Bundle中獲取數(shù)據(jù),重新顯示在Activity A中
- 在Activity B中獲取Activity A傳過去的Intent對象,并取出Bundle對象,再從Bundle中取出數(shù)據(jù)字段,顯示在當(dāng)前頁面
- Activity B中也有一個(gè)Button,點(diǎn)擊Button后,調(diào)用setResult傳回結(jié)果,并關(guān)閉當(dāng)前頁面。因此,看起來的效果就是回到了Activity A
源碼如下:
1、Activity A的實(shí)現(xiàn):
public class ExampleActivity extends Activity { private EditText mEditText; private RadioButton mRb1; private RadioButton mRb2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_page_layout); Button button = findViewById(R.id.buttonGoToLayout2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mEditText = findViewById(R.id.editText); // 獲取輸入的身高 double height = Double.parseDouble(mEditText.getText().toString()); // 獲取性別 String gender = ""; mRb1 = findViewById(R.id.radioButtonMale); mRb2 = findViewById(R.id.radioButtonFemale); if (mRb1.isChecked()) { gender = "M"; } else { gender = "F"; } Intent intent = new Intent(ExampleActivity.this, SecondActivity.class); // 將數(shù)據(jù)傳入第二個(gè)Activity Bundle bundle = new Bundle(); bundle.putDouble("height", height); bundle.putString("gender", gender); intent.putExtras(bundle); startActivityForResult(intent, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == 0) { Bundle bundle = data.getExtras(); double height = bundle.getDouble("height"); String gender = bundle.getString("gender"); mEditText.setText("" + height); if (gender.equals("M")) { mRb1.setChecked(true); } else { mRb2.setChecked(true); } } } }
2、布局文件main_page_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="計(jì)算標(biāo)準(zhǔn)體重" android:paddingTop="20dp" android:paddingLeft="20dp" android:textSize="30sp"/> <TextView android:text="性別:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_alignStart="@id/textView1" android:layout_marginTop="38dp" android:layout_below="@id/textView1" android:layout_marginStart="46dp"/> <TextView android:text="身高:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_alignStart="@id/textView1" android:layout_marginStart="46dp" android:layout_below="@id/textView3" android:layout_marginTop="29dp"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_toEndOf="@id/textView4" android:layout_marginStart="36dp" android:autofillHints="@string/app_name" android:hint="0" android:layout_alignBaseline="@id/textView4"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="厘米" android:layout_alignBaseline="@id/editText" android:layout_toRightOf="@id/editText" android:layout_marginStart="10dp" /> <RadioButton android:layout_below="@id/textView1" android:id="@+id/radioButtonMale" android:text="男" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@id/textView1" android:layout_marginTop="30dp" android:layout_marginStart="113dp"/> <RadioButton android:id="@+id/radioButtonFemale" android:text="女" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView1" android:layout_toEndOf="@id/radioButtonMale" android:layout_marginLeft="15dp" android:layout_marginTop="30dp" android:layout_marginStart="49dp"/> <Button android:text="計(jì)算" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonGoToLayout2" android:layout_marginTop="90dp" android:layout_below="@id/radioButtonMale" android:layout_alignStart="@id/textView1" android:layout_marginStart="92dp"/> </RelativeLayout>
3、Activity B的實(shí)現(xiàn):
public class SecondActivity extends Activity { private Intent mIntent; private Bundle mBundle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); mIntent = getIntent(); mBundle = mIntent.getExtras(); // 記得判空 if (mBundle == null) { return; } // 獲取Bundle中的數(shù)據(jù) double height = mBundle.getDouble("height"); String gender = mBundle.getString("gender"); // 判斷性別 String genderText = ""; if (gender.equals("M")) { genderText = "男性"; } else { genderText = "女性"; } // 獲取標(biāo)準(zhǔn)體重 String weight = getWeight(gender, height); // 設(shè)置需要顯示的文字內(nèi)容 TextView textView = findViewById(R.id.textView2); textView.setText("你是一位" + genderText + "\n你的身高是" + height + "厘米\n你的標(biāo)準(zhǔn)體重是" + weight + "公斤"); Button button = findViewById(R.id.buttonGoBack); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 設(shè)置結(jié)果,并關(guān)閉頁面 setResult(RESULT_OK, mIntent); finish(); } }); } // 四舍五入格式化 private String format(double num) { NumberFormat formatter = new DecimalFormat("0.00"); return formatter.format(num); } // 計(jì)算標(biāo)準(zhǔn)體重的方法 private String getWeight(String gender, double height) { String weight = ""; if (gender.equals("M")) { weight = format((height - 80) * 0.7); } else { weight = format((height - 70) * 0.6); } return weight; } }
4、Activity B的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="This is the second layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:paddingTop="30dp" android:paddingStart="50dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonGoBack" android:text="回到上一頁" android:layout_alignStart="@id/textView2" android:layout_below="@id/textView2" android:layout_marginTop="54dp" android:layout_marginStart="52dp"/> </RelativeLayout>
不過這里有3個(gè)地方需要注意:
1.startActivityForResult的第二個(gè)參數(shù)requestCode傳的是0,那么我們分別看下傳遞的值小于0和大于0是什么結(jié)果:
(1)傳一個(gè)小于0的值,比如-1:等同于調(diào)用 startActivity,onActivityResult不會被調(diào)用
(2)傳一個(gè)大于0的值,比如1:效果等同于傳0,onActivityResult的第一個(gè)參數(shù)正是我們通過startActivityForResult傳遞的requestCode
2.onActivityResult的第二個(gè)參數(shù)resultCode:它是第二個(gè)activity通過setResult返回的,常用的取值有2個(gè):RESULT_CANCELED、RESULT_OK
(1)RESULT_CANCELED:Activity B拉起失敗,比如crash
(2)RESULT_OK:Activity B操作成功后的返回值
還有一個(gè)不太常用的取值:RESULT_FIRST_USER,Android源碼對這個(gè)取值的定義是“user-defined activity results”(用戶自定義的),我在源碼中全局搜索了下,用的地方不多,挑了一兩個(gè)使用的地方:
(1)PackageInstaller下面的InstallFailed.java(安裝apk失敗的相關(guān)頁面)
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); int statusCode = getIntent().getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE); if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) { // …….. setResult(Activity.RESULT_FIRST_USER, result); finish(); }
(2)PackageInstaller下面的InstallStaging.java
private void showError() { (new ErrorDialog()).showAllowingStateLoss(getFragmentManager(), "error"); // ……. setResult(RESULT_FIRST_USER, result); }
PackageInstaller下面的UninstallerActivity.java(卸載apk的相關(guān)頁面):在onCreate方法里面有多處設(shè)置為RESULT_FIRST_USER。
因此,我的理解是業(yè)務(wù)自身在一些錯(cuò)誤或無效的場景下使用,由業(yè)務(wù)自己定義。
3. 如果啟動Activity B時(shí)設(shè)置了new_task啟動模式,進(jìn)入Activity B后,Activity A會立即回調(diào)onActivityResult,而且resultCode是0;從Activity B setResult返回后,不再有onActivityResult的回調(diào)!
以上就是Android利用startActivityForResult返回?cái)?shù)據(jù)到前一個(gè)Activity的詳細(xì)內(nèi)容,更多關(guān)于Android 返回?cái)?shù)據(jù)到前一個(gè)Activity的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android仿淘寶view滑動至屏幕頂部會一直停留在頂部的位置
這篇文章主要介紹了Android仿淘寶view滑動至屏幕頂部會一直停留在頂部的位置的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android編程實(shí)現(xiàn)的手寫板和涂鴉功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)的手寫板和涂鴉功能,涉及Android界面布局及圖形繪制功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01Android TextView設(shè)置不同的顏色字體
這篇文章主要為大家詳細(xì)介紹了Android TextView設(shè)置不同的顏色字體,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Kotlin實(shí)現(xiàn)圖片選擇器的關(guān)鍵技術(shù)點(diǎn)總結(jié)
這篇文章主要給大家介紹了關(guān)于Kotlin實(shí)現(xiàn)圖片選擇器的一些關(guān)鍵技術(shù)點(diǎn),這是一個(gè)我在學(xué)習(xí)Kotlin過程中的一個(gè)練手項(xiàng)目,非常適合學(xué)習(xí)Kotlin的時(shí)候參考,需要的朋友可以參考下2021-09-09Linux系統(tǒng)下安裝android sdk的方法步驟
這篇文章主要介紹了Linux系統(tǒng)下安裝android sdk的方法步驟,文中介紹的非常詳細(xì),相信對大家具有一定的參考價(jià)值,需要的朋友可以們下面來一起看看吧。2017-03-03