Monday, 3 December 2018

OnActivityResult not called when starting a dialog activity and recreating activities

I have two activities, A and B. A starts B by calling startActivityForResult(). B has theme @android:style/Theme.Dialog.

Thus, B is displayed "on top of" A, but A is still visible (as B is a dialog).

When both activities are launched, I force recreation by switching to another task and back (I have enabled the "Don't keep activities" option in the android developer settings. I see OnCreate is called on A and B when I return to my task.)

When I click the button in Activity B, it calls setResult() and finish(), but onActivityResult() is not called on A.

The problem does not appear

  • if I do not force recreation of the activities

or

  • if I remove the dialog theme from Activity B.

I tested this on a Google Pixel with Android 9.

Is this expected behavior or a bug in Android?


This is the code I used to test this (Xamarin Android):

[Activity(Label = "@string/app_name")]
public class ActivityA : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        Kp2aLog.Log("OnCreate A");
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.yubichall_test);

        {
            FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Start B";
            FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
            {

                var chalIntent = new Intent(this, typeof(ActivityB));

                StartActivityForResult(chalIntent, 123);

            };
        }


    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Android.Util.Log.Debug("KP2A", "OnActivityResult A: " + requestCode);

    }
}
[Activity(Label = "@string/app_name", Theme = "@android:style/Theme.Dialog")]
public class ActivityB : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        Kp2aLog.Log("OnCreate B");
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.yubichall_test);

        FindViewById<Button>(Resource.Id.btn_yubichall).Text = "Return result to A";

        {
            FindViewById<Button>(Resource.Id.btn_yubichall).Click += (sender, args) =>
            {
                SetResult(Result.Ok);
                Finish();

            };
        }


    }
}

where the layout yubicall_test is

    <?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:orientation="vertical"
              android:layout_height="wrap_content">
  <Button android:id="@+id/btn_yubichall"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="yubi challenge"
  />

  <TextView
    android:id="@+id/text_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  />

</LinearLayout>



from OnActivityResult not called when starting a dialog activity and recreating activities

No comments:

Post a Comment