質問

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:

  1. Are my worries correct?
  2. If yes, I would like to know what kind of bad things could happen in my App by using this procedure?
  3. Maybe some sample code of doing it right in a clean and also short way?
役に立ちましたか?

解決

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();

他のヒント

The Dialog class is a base class for showing dialogs. You should use a more specific subclass for displaying a Yes/No dialog. For example: AlertDialog.

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
    builder.setMessage(R.string.dialog_fire_missiles)
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and show it
    builder.create().show();

You can find more details here.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top