Ok guys, this is my first question on SO and I would like know, if it is really necessary to use DialogFragment as a Container for my simple custom Dialog that I have in my Activity.
Here is my code:
public class MainActivity extends Activity
{
// VARS:
private Button buttonShowDialog;
private Dialog dialogSimple;
private Button buttonOK;
private Button buttonCancel;
// ONCREATE:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
buttonShowDialog = (Button) findViewById(R.id.button_showdialog);
buttonShowDialog.setOnClickListener(new OnClickListener)
{
@Override
public void onClick(View view)
{
if (dialogSimple != null)
{
dialogSimple.show();
}
else
{
showDialog();
}
}
});
}
// SHOW DIALOG:
private void showDialog()
{
dialogSimple = new Dialog(MainActivity.this);
dialogSimple.setContentView(R.layout.dialog);
buttonOK = (Button) dialogSimple.findViewById(R.id.button_ok);
buttonCanel = (Button) dialogSimple.findViewById(R.id.button_cancel);
buttonOK.setOnClickListener(new OnClickListener)
{
@Override
public void onClick(View view)
{
doSomeStuff();
}
});
buttonCanel.setOnClickListener(new OnClickListener)
{
@Override
public void onClick(View view)
{
dialogSimple.dismiss();
}
});
dialogSimple.show();
}
}
My manifest entry for the activity:
android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
I tested and it worked fine with: API 8 (Device), 9 (Device), 10 (Emu), 11 (Emu), 16 (Emu), 17 (Device), 18 (Device) and 19 (Emu)
So my worries are, that with this procedure my App could misbehave on some devices, because the Dialog is not in a DialogFragment.
My Questions:
No, not required. Just recommended.
But you can achieve it with an alert dialog:
AlertDialog.Builder bld = new AlertDialog.Builder();
bld.setMessage(...);
bld.setPositiveButton(...);
bld.setNegativeButton(...);
bld.create().show();