Android O中DialogFragment中onActivityResult的不同行为

时间:2017-07-31 15:52:23

标签: android android-dialogfragment onactivityresult android-8.0-oreo

startActivityForResult拨打DialogFragment时,我发现Android 7(以及之前)和Android 8(测试版)之间存在差异。

在下面的MainActivity中,当' OK'按下对话框,它会自动关闭,创建并显示一个新对话框,并为startActivityForResult调用AnotherActivityAnotherActivity立即结束。

在Android 7上,在新对话框实例上调用onActivityResult。在Android 8上,永远不会调用onActivityResult

任何人都能解释这种差异吗?

MCVE

public class MainActivity extends AppCompatActivity {

    private static final String FRAGMENT_TAG = "tag";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        new SomeDialogFragment().show(getFragmentManager(), FRAGMENT_TAG);
    }

    public static class SomeDialogFragment extends DialogFragment
    {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            super.onCreateDialog(savedInstanceState);

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setPositiveButton(
                    "OK",
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface d, int which)
                        {
                            dismiss();
                            SomeDialogFragment dialog = new SomeDialogFragment();
                            dialog.show(getFragmentManager(), FRAGMENT_TAG);
                            startActivityForResult(new Intent(getActivity(), AnotherActivity.class), 7);
                        }
                    });

            return builder.create();
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            // Called on Android 7 but not Android 8.
        }
    }
}

public class AnotherActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setResult(1);
        finish();
    }
}

0 个答案:

没有答案