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
Android. Scrolling 2 Listviews Together
Android Activity Has Leaked Window Com.Android.Internal.Policy.Impl.Phonewindow$Decorview Issue
Load Large Images with Picasso and Custom Transform Object
Set Font for All Textviews in Activity
Android Spannablestring Set Background Behind Part of Text
Android:How to Update the Selector(Statelistdrawable) Programmatically
Measuring a View Before Rendering It
Whatsapp Like Collapsing Toolbar
Clear Text in Edittext When Entered
Android - Build a Notification, Taskstackbuilder.Addparentstack Not Working
How to Attach Android Source to Eclipse
Add Elevation/Shadow on Toolbar for Pre-Lollipop Devices
How to Set Spinner Default Value to Null
How to Show Progress Dialog in Android
Textinputlayout :How to Give Padding or Margin to Hint
How to Take Multiple Photos Before Dismissing Camera Intent