在启动对话活动并重新创建活动时未调用OnActivityResult

Phi*_*ipp 5 android dialog xamarin.android android-lifecycle

我有两个活动,A和B. A调用了B startActivityForResult().B有主题@android:style/Theme.Dialog.

因此,B显示在"A"之上,但A仍然可见(因为B是对话框).

当两个活动都启动时,我通过切换到另一个任务并返回来强制重新创建(我在android开发人员设置中启用了"不要保持活动"选项.当我返回到我的任务时,我看到在A和B上调用OnCreate .)

当我点击活动B按钮,它调用setResult()finish(),但onActivityResult() 不叫上.

问题没有出现

  • 如果我不强制重新开展活动

要么

  • 如果我从活动B中删除对话框主题.

我在使用Android 9的Google Pixel上测试了这个.

这是预期的行为还是Android中的错误?


这是我用来测试它的代码(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();

            };
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

布局yubicall_test的位置

    <?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>
Run Code Online (Sandbox Code Playgroud)