[Xamarin] 透過StartActivityForResult傳值回來

2013-07-11

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

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

這次案例首先主畫面為.

2013-07-11_171045



點下按鈕後,就會開啟 LayoutAskQuestion.axml
2013-07-11_171040

然後就會回到主要的畫面,並且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>

2013-07-11_171045

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>

2013-07-11_171040

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();
            };
 
        }
    }
}

呼叫結果..
Screenshot_2013-07-11-17-29-06

很簡單,也很方便,如果有說不清楚的地方就看Source code 搂.
這篇文章也寫得很簡單易懂 http://hatsukiakio.blogspot.tw/2009/06/startactivitystartactivityforresult.html


Source code:



當麻許的超技八 2014 | Donma Hsu Design.