数日前、アラート ダイアログAlertDialogに関する質問をしましたが、これはバグですか? 質問をする前に、アラート ダイアログに関する多くの投稿を調べました。私が読んだすべての質問で、すべて show() を使用してダイアログを表示します。質問者が投稿されたコードをアプリで実際に使用しているかどうかはわかりません。その場合、アクティビティが縦または横のみでない限り、showDialog(int id) を使用してダイアログを表示する必要があります。これは、showDialog を使用している場合にのみ、OS が向きの変更を処理するためです。これを以下のコードに示します。ボタンをクリックしてデバイスを回転させるだけで、何が起こるかを確認できます。
レイアウト
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/with_show_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clickable = "true"
android:text="With Show"
/>
<Button
android:id="@+id/no_show_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/with_show_btn"
android:layout_alignParentBottom="true"
android:clickable = "true"
android:text="No Show"
/>
<Button
android:id="@+id/no_on_create_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/no_show_btn"
android:layout_alignParentBottom="true"
android:clickable = "true"
android:text="Not Using onCreateDialog"
/>
</RelativeLayout>
コード
public class AlertAndShowActivity extends Activity implements OnClickListener
{
static final int DIALOG_SHOW_ID = 1;
static final int DIALOG_NO_SHOW_ID = 2;
static final int DIALOG_NOT_USING_ONCREATEDIALOG_ID = 3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button showBtn = (Button) findViewById(R.id.with_show_btn);
showBtn.setOnClickListener(this);
Button noShowBtn = (Button) findViewById(R.id.no_show_btn);
noShowBtn.setOnClickListener(this);
Button notUsingOnCreateDialogBtn = (Button) findViewById(R.id.no_on_create_btn);
notUsingOnCreateDialogBtn.setOnClickListener(this);
}
protected AlertDialog createDialog(final int id)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dismiss is not enough");
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// When dialog is showing through showDialog(int id)
// and show() is called, then calling cancel will dismiss
// the dialog but it will reappear on orientation change.
// When you run this, chick WithShow and then click Cancel
// and rotate your device to see what I mean.
dialog.cancel();
}
});
builder.setNegativeButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// system will automatically call dismiss
// see also remark for the cancel case above.
}
});
builder.setNeutralButton("Good", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// This will produce the behavior that one wants when show()
// is used in the code. That is the dialog will not reappear
// on orientation change.
removeDialog(id);
}
});
return builder.create();
}
protected AlertDialog createDialogWithShow(int id)
{
AlertDialog alertDlg = createDialog(id);
// Only call show if you want to disable a button.
// You can only disable a button after calling show(),
// otherwise you will get null pointer exception.
alertDlg.show();
//alertDlg.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
return alertDlg;
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
// notUsingOnCreateDialogBtn
case R.id.no_on_create_btn:
// this will create the dialog and show it on the UI
// but if one rotates the device it will not be
// automatically recreated.
createDialogWithShow(DIALOG_NOT_USING_ONCREATEDIALOG_ID);
break;
// withShowBtn
case R.id.with_show_btn:
// OS will recreate the dialog on orientation change.
showDialog(DIALOG_SHOW_ID);
break;
case R.id.no_show_btn:
showDialog(DIALOG_NO_SHOW_ID);
}
}
@Override
protected Dialog onCreateDialog(int id)
{
// no case here for DIALOG_NOT_USING_ONCREATEDIALOG_ID
// since it is not shown using showDialog
switch (id)
{
case DIALOG_NO_SHOW_ID:
return createDialog(id);
case DIALOG_SHOW_ID:
return createDialogWithShow(DIALOG_SHOW_ID);
default:
return super.onCreateDialog(id);
}
}
}