上一篇文章(開啟另外一個Activity 並且帶資料),提到了開啟一個新的Activity ,我們將值透過intent 帶到下個Activity

但是,如果我們開啟的Actrivity其實是有一個任務的,他必須要回傳值回來,讓父親可以知道一些訊息可以帶回來,我們該如何做

這次案例首先主畫面為.

點下按鈕後,就會開啟 LayoutAskQuestion.axml


然後就會回到主要的畫面,並且Toast剛剛選擇的結果.

內容我就寫在code  註解裡..

主畫面 Main.axml :

<?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/btnAskQuestion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="詢問問題" />
</LinearLayout>

Activity1.cs:

using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
 
namespace TestStartActivityForResult
{
    [Activity(Label = "TestStartActivityForResult", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            SetContentView(Resource.Layout.Main);
 
            
            var btnAskQuestion = FindViewById<Button>(Resource.Id.btnAskQuestion);
            //詢問的按鈕按下後
            btnAskQuestion.Click += delegate
            {
                //因為期許他將回傳值,所以使用StartActivityForResult 叫起
                //第二參數為 requestcode 這邊主要是設定讓 OnActivityResult 可以判斷當初發出的動機
                StartActivityForResult(typeof(ActivityAskQuestion), 1);
            };
        }
 
        /// <summary>
        /// 當有 AcrivityForReult Activity 被呼叫且結束後
        /// </summary>
        /// <param name="requestCode"></param>
        /// <param name="resultCode"></param>
        /// <param name="data"></param>
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
 
            //如果當初的發的requestCode =1
            if (requestCode == 1 && resultCode == Result.Ok)
            {
                Toast.MakeText(this, "選取結果(OnActivityResult):" + data.GetStringExtra("hero"), ToastLength.Short).Show();
            }
        }
    }
}
 

被呼叫端 LayoutAskQuestion.axml :

<?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/btnBlackWidow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="黑寡婦" />
    <Button
        android:id="@+id/btnIronMan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="鋼鐵人" />
</LinearLayout>


ActivityAskQuestion.cs:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
 
namespace TestStartActivityForResult
{
    [Activity(Label = "Son Activity")]
    public class ActivityAskQuestion : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.LayoutAskQuestion);
 
            var btnBlackWidow = FindViewById<Button>(Resource.Id.btnBlackWidow);
            btnBlackWidow.Click += delegate
            {
                //開啟一個新的intent
                var intent = new Intent(this, typeof(Activity1));
                //放入一個key 為hero 值為 黑寡婦
                intent.PutExtra("hero", "黑寡婦");
                //狀態設為OK
                SetResult(Result.Ok, intent);
                //呼叫後將關閉此視窗
                Finish();
            };
 
            var btnIronMan = FindViewById<Button>(Resource.Id.btnIronMan);
            btnIronMan.Click += delegate
            {
                var intent = new Intent(this, typeof(Activity1));
                intent.PutExtra("hero", "鋼鐵人");
                SetResult(Result.Ok, intent);
                Finish();
            };
 
        }
    }
}

呼叫結果..

[Xamarin] 透過StartActivityForResult傳值回來(转贴)的更多相关文章

  1. [Xamarin] 透過 intent-filter 來接管 http ,製作偽瀏覽器 (转帖)

    使用Android 的朋友一定對這畫面不陌生在開啟網址的時候,或是Youtube連結的時候,因為Android 發現,你手機安裝的App有哪些可以支援這些東西的瀏覽 所以,就可以使用甚麼東西來進行開啟 ...

  2. [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)

    今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...

  3. [Xamarin] 透過WebClient跟網路取得資料 (转帖)

    之前寫過一篇文章,關於在Android上面取得資料 透過GET方式傳資料給Server(含解決中文編碼問題) 我們來回顧一下 Android 端的Code: 有沒有超多,如果是在Xaramin下面,真 ...

  4. [Xamarin] 透過 IsolatedStorageFile儲存資料(转帖)

    開發手機App通常都會遇到想要儲存資料的,舉個例來說,像是 (圖片來源:http://docs.xamarin.com/guides/android/application_fundamentals/ ...

  5. 使用 ssmtp 於 shell 透過 Gmail 寄信

    有很多程式於 bash shell 執行, 執行完要自動寄信出去, 但是最近都被 Google 退信, 最好的方法是透過 Gmail 直接寄信. 本來是要另外寫隻 script 來做這種事, 剛剛發現 ...

  6. (STM32F4) 精準的Delay不透過Timer

    從一個厲害的國外工程師看來的delay寫法,使用while loop會使用幾個指令去計算,可能會需要多少時間. while(variable--); 這行代碼執行一次預估會消耗MCU 4 clock ...

  7. [C#] 與Android共舞–透過GET方式傳資料給Server(含解決中文編碼問題) (转帖)

    上一篇文章分享了透過POST 方式傳資料回Server,這一篇來談談有關於透過GET的方式傳遞 首先,如我預期的一樣,透過網址傳遞,會產生編碼問題,這邊我就順代解掉,希望有碰到的人 可以不用為此煩惱. ...

  8. [SQL]透過redgate SQL Monitor 來找出 ASYNC_NETWORK_IO 問題

    原文:[SQL]透過redgate SQL Monitor 來找出 ASYNC_NETWORK_IO 問題 最近因為在查一個SQL的效能問題,透過 sys.dm_os_wait_stats 來取得To ...

  9. 透過手機 App 在 OpenELEC(XBMC)中輸入中文

    這裡介紹如何使用手機 App 在沒有中文輸入法的 OpenELEC(XBMC)中輸入中文字. OpenELEC(XBMC)雖然有內建中文語系,但是卻沒有中文的輸入法,沒辦法直接輸入中文字,這對於一般家 ...

随机推荐

  1. 推送安霸A7L实时视频至RTMP服务器(1)

    使用librtmp进行H264与AAC直播 (转:http://www.codeman.net/2014/01/439.html) 1.帧的划分 1.1 H.264帧 对于H.264而言每帧的界定符为 ...

  2. Flash的不同位宽与CPU地址线的接线问题?

    一般Flash都有8.16.32等这些不同的位宽,当然说白了就是Flash的数据线位数. 在Flash与CPU的地址线的连接问题时:不同位宽的有不同的连接方法: 一般是:位宽为8时CPU的ADDR0与 ...

  3. linux每天一小步---ls命令详解

    1 命令功能: 列出当前目录下或者指定目录下的所有文件和目录,ls是list的缩写. 2 命令语法: ls [选项] [目录名]     #注:[]中的内容为非必选项 3 命令选项: -a 列出目录下 ...

  4. hdu2364之BFS

    Escape Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. [翻译] FastReport Class Hierarchy (FastReport 组件类层次结构)

    "TfrxComponent" is the base class for all FastReport components. Objects of this type have ...

  6. [leetcode] 9. Binary Tree Level Order Traversal

    跟第七题一样,把最后的输出顺序换一下就行... Given a binary tree, return the level order traversal of its nodes' values. ...

  7. Centos7下修复 视频播放器(先 安装VLC视频播放器)

    用最新的CentOS7发现没有视频播放器,于是在http://pkgs.org/上查找,发现了nux dextop仓库上有, 于是到他的官网上http://li.nux.ro/repos.html查了 ...

  8. Apache commons StringUtils 在运行时出现NoClassDefError错误的解决方法

    Apache commons StringUtils 在运行时出现NoClassDefError错误的解决方法 在用tomcat运行WEB项目,并且使用了StringUtils包的时候,会出现 jav ...

  9. 3:C#异步WaitAll的使用

    编写界面如图: private async void button1_Click(object sender, EventArgs e) { #region 单个执行的异步,效率慢 HttpClien ...

  10. django drf 自定义jwt用户验证逻辑

    新建Backend类 from django.contrib.auth.backends import ModelBackend from django.shortcuts import render ...