从startActivityForResult
拨打DialogFragment
时,我发现Android 7(以及之前)和Android 8(测试版)之间存在差异。
在下面的MainActivity
中,当' OK'按下对话框,它会自动关闭,创建并显示一个新对话框,并为startActivityForResult
调用AnotherActivity
。 AnotherActivity
立即结束。
在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();
}
}