OnActivityResult和OnResume的执行顺序

时间:2013-05-02 14:30:06

标签: android android-activity xamarin.android

我知道OnActivityResult应该在一个Activity中的OnResume之前立即调用,但这不会发生在我身上。我发现它在OnResume之前被调用,甚至在OnStart之前。我的示例代码如下,我正在查看相关方法,以了解发生了什么。发生了什么事?

活动1

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.PM;

namespace LifecycleTest
{
    [Activity(MainLauncher = true)]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            this.SetContentView(Resource.Layout.Main);

            this.FindViewById<Button>(Resource.Id.button1).Text = "Start Activity 2";
            this.FindViewById<Button>(Resource.Id.button1).Click += button_Click;
        }

        void button_Click(object sender, EventArgs e)
        {
            Intent i = new Intent(this, typeof(Activity2));
            this.StartActivityForResult(i, 1);
        }

        protected override void OnStop()
        {
            base.OnStop();
        }

        protected override void OnResume()
        {
            base.OnResume();
        }

        protected override void OnStart()
        {
            base.OnStart();
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
        }
    }
}

活动2

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.PM;

namespace LifecycleTest
{
    [Activity]
    public class Activity2 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            this.SetContentView(Resource.Layout.Main);

            this.FindViewById<Button>(Resource.Id.button1).Text = "Return Result";
            this.FindViewById<Button>(Resource.Id.button1).Click += button_Click;
        }


        void button_Click(object sender, EventArgs e)
        {
            this.SetResult(Result.Ok);
            this.Finish();
        }
    }
}

布局

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <Button
        android:text="Button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />
</LinearLayout>

2 个答案:

答案 0 :(得分:7)

查看API-23 android.app.ActivityThread#performResumeActivity,它会调用#deliverResults() before doing Activity#performResume()

前者最终会调用Activity#onActivityResult(),而后者会调用Activity#onStart(),如果它尚未启动。因此,调用的顺序将是:

onActivityResult()
onStart() // only if it has been stopped
onResume()

这似乎是一直回到Android 2.0的情况。虽然有帖子claim otherwise

答案 1 :(得分:1)

当您的活动设置为“singleInstance”时会发生这种情况。你的活动是singleInstance吗?尝试删除它。