How to Show Dialog in Oncreate Method

AlertDialog won't show in onCreate method

The create method return an instance of AlertDialog

Instead of initializing it to the Dialog when you call the create method of the AlertDialog

Dialog dialog = startGameDialog.create();
dialog.show();

pass it to the AlertDialog

AlertDialog alertDialog = startGameDialog.create();
alertDialog.show();

use the currents activity context rather than using the whole application contex.t

Context context = Your_activity.this;

Want to display AlertDialog in onCreate of Activity - android

you're performing your long operations in UI thread. Move them to the AsyncTask's doInBackground method. See the example here.

displaying a dialog in onCreate()

If you want to run code only after a button has been pressed, you should use an `onClickListener':

See: http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int, java.lang.CharSequence, android.content.DialogInterface.OnClickListener)

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.first_run_version_title)
.setNeutralButton(R.string.ok_menu_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code to run here
}
});
AlertDialog alert = builder.create();
alert.show(); // <-- Forgot this in the original post

AlertDialog with custom view in onCreate()

It will crash as the UI of the activity is not yet prepared, and activity is not yet shown to the user, android shows the screen after its onResume() execution is completed, try showing the dialog from onResume()

How to display dialog on any screen (fragment or activity) in android

Create a transparent activity and in its onCreate method show your dialog.
Set an OnDismissListener for the dialog and in this callback finish the activity.
Every where you need showing your dialog you can start this transparent activity



Related Topics



Leave a reply



Submit