android startActivityForResult的使用方法介紹
Activity 跳轉(zhuǎn) 都知道用startActivity(Intent)
但是如果下面情況呢?
Activity1 跳轉(zhuǎn)到 Activity2 但是還需要在Activity2 再回到 Activity1呢? 可能有人說: 那我在Activity2 再使用 startActivity() 不就可以了 是的 但是 startActivityForResult() 能夠直接完成這項(xiàng)工作
[示例]
Activity1: 有2個(gè)EditText 用于接收用戶輸入的2個(gè)字符串 要求把這2個(gè)字符串連接起來 我現(xiàn)在把連接的工作交給 Activity2 來做 并且把連接好后的字符串再返回給 Activity1 并由它負(fù)責(zé)顯示
[代碼]
1. 構(gòu)建 Activity1 所需的界面
Java代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...is waiting"
/>
</LinearLayout>
2. 得到2個(gè)EditText的用戶輸入
first = (EditText) findViewById(R.id.first);
second = (EditText) findViewById(R.id.second);
3. 把字符串裝入Bundle 再放置于Intent 然后發(fā)送之
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putString("first", first.getText().toString());
b.putString("second", second.getText().toString());
i.putExtras(b);
startActivityForResult(i,10);
補(bǔ)充:
public void startActivityForResult (Intent intent, int requestCode)
Intent intent:系統(tǒng)會(huì)根據(jù)這個(gè)確定目的Activity
int requestCode:用于標(biāo)識(shí)該Intent 回來后確定是不是想要的返回
4. 注冊(cè)View監(jiān)聽器
findViewById(R.id.start).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
sendCalculate();
}
});
5. 構(gòu)建 Activity2 的界面 把處理的結(jié)果返回
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/reply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="reply"
/>
</LinearLayout>
6. 得到傳入的Intent 以及傳過來的2個(gè)字符串 并連接之
Intent i = this.getIntent();
Bundle b = i.getExtras();
String v1 = b.getString("first");
String v2 = b.getString("second");
value = v1 + v2;
7. 定義Intent 并存放返回結(jié)果 并返回之
Intent i = new Intent();
Bundle b = new Bundle();
b.putString("CALCULATION", value);
i.putExtras(b);
this.setResult(RESULT_OK, i);
this.finish();
8. 事情完成了么? 當(dāng)然沒有 別忘了 Activity1 還要接收數(shù)據(jù)并顯示之
protected void onActivityResult(int requestCode, int resultCode,
Intent data){
switch (resultCode){
case RESULT_OK:
Bundle b = data.getExtras();
String string = b.getString("CALCULATION");
updateText(string);
}
}
- Android利用startActivityForResult返回?cái)?shù)據(jù)到前一個(gè)Activity
- Android startActivityForResult和setResult的區(qū)別
- Android startActivityForResult實(shí)例詳解
- Android基礎(chǔ)之startActivityForResult()的用法詳解
- android開發(fā)教程之startActivityForResult使用方法
- 基于android startActivityForResult的學(xué)習(xí)心得總結(jié)
- Android startActivityForResult的基本用法詳解
相關(guān)文章
Android中NavigationView的使用與相關(guān)問題解決
大家都知道NavigationView的引入讓 Android側(cè)邊欄實(shí)現(xiàn)起來相當(dāng)方便,最近公司項(xiàng)目中也使用這個(gè)新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個(gè)部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。2016-10-10Android實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊輸入框
如今市面上APP的輸入框可以說是千奇百怪,不搞點(diǎn)花樣出來貌似代表格局沒打開,還在使用系統(tǒng)自帶的輸入框的兄弟可以停下腳步,哥帶你實(shí)現(xiàn)一個(gè)簡(jiǎn)易的帶邊框輸入框,感興趣的同學(xué)可以自己動(dòng)手試一下2023-08-08ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來用)
這篇文章主要介紹了ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來用)的相關(guān)資料,需要的朋友可以參考下2016-07-07Android 下載網(wǎng)絡(luò)圖片并顯示到本地
本文主要介紹了Android實(shí)現(xiàn)下載網(wǎng)絡(luò)圖片并顯示到本地功能的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03Android Jetpack Compose實(shí)現(xiàn)列表吸頂效果
安卓傳統(tǒng)的Recyclerview打造懸浮頭部StickyHeader的吸頂效果,十分麻煩,而在Compose中就簡(jiǎn)單多了。因此,本文將采用Jetpack Compose實(shí)現(xiàn)列表吸頂效果,需要的可以參考一下2022-02-02Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面
這篇文章主要介紹了Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除
本篇文章主要介紹了android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10