3

I understand that OnActivityResult should be called immediately before OnResume in an Activity but that's not happening for me. I'm finding it gets called before OnResume, and even before OnStart. My sample code is below, I'm breakpointing the relevant methods to see what's happening. What's going on?

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

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

Layout

<?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>
ctacke
  • 66,480
  • 18
  • 94
  • 155
Martynnw
  • 10,625
  • 5
  • 28
  • 27
  • This question must be marked as dublicate. You can see answers in the following topics: http://stackoverflow.com/questions/6468319/onactivityresult-onresume and http://stackoverflow.com/questions/4253118/is-onresume-called-before-onactivityresult – kolobok Jul 03 '13 at 15:07
  • 1
    @akapelko it is not, this topic deals with it being called *before* `onStart()` which is nowhere documented and the other topics discuss it being called before `onResume()` which is documented – rndstr Nov 07 '15 at 11:08

2 Answers2

8

Looking at API-23 android.app.ActivityThread#performResumeActivity it calls #deliverResults() before doing Activity#performResume().

The former eventually calls Activity#onActivityResult() while the latter will call Activity#onStart() if it hasn't been started. Thus, the order of invocation will be:

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

This seems to be the case all the way back to Android 2.0. Although there are posts that claim otherwise.

Community
  • 1
  • 1
rndstr
  • 777
  • 8
  • 14
  • This isn't hard proof of your order. There is a function `Activity#performStart` which could be called from somewhere before `ActivityThread#performResumeActivity`. – androidguy Aug 13 '17 at 03:19
1

This happens when your activity is set as "singleInstance". Is your activity singleInstance. Try removing it.

Sagar Waghmare
  • 4,702
  • 1
  • 19
  • 20