Having a dialog box in the middle of what the user is doing is a hostile task. What if they play Angry Birds and you just destroyed them? Only a platform can and should get away with it.
Use the notification area, for example Google, if you donβt have an application right now (your activity is in working order). Then simply use this activity context to open AlertDialog() . You can determine if the operation works by overriding onResume() and onPause() - everything that happens between them is the time of your activity.
AlertDialog can be displayed as follows:
new AlertDialog.Builder(Ctxt) //Use an activity object here .setMessage(R.string.MyMessageID) //Provide a message here... A string or a string ID will do .setCancelable(true) //If you want them to be able to dismiss with a Back button .setNegativeButton(R.string.IDS_NO, null) //No action on NO, right? .setPositiveButton(R.string.IDS_YES, OnYesClickListener) //Plug your own listener... .create() .show();
For a simple Yes / No message / dialog, AlertDialog enough. For a more complex user interface, derive a class from Dialog and create your own layout.
source share