How To Show Dialog Box From The Class Which Extends Application In Android?
Solution 1:
Your program may behave as you want!**
** Just remember that you need to think about the consequences of its actions.
publicclassMyApplicationextendsApplication {
/**
* show example alertdialog on context -method could be moved to other class
* (eg. MyClass) or marked as static & used by MyClas.showAlertDialog(Context)
* context is obtained via getApplicationContext()
*/publicvoidshowAlertDialog(Context context) {
/** define onClickListener for dialog */
DialogInterface.OnClickListener listener
= new DialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog, int which) {
// do some stuff eg: context.onCreate(super)
}
};
/** create builder for dialog */
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(false)
.setMessage("Messag...")
.setTitle("Title")
.setPositiveButton("OK", listener);
/** create dialog & set builder on it */
Dialog dialog = builder.create();
/** this required special permission but u can use aplication context */
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
/** show dialog */
dialog.show();
}
@Override
publicvoidonCreate() {
showAlertDialog(getApplicationContext());
}
}
imports for abowe:
import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
edity:
You cannot **display an application window/dialog through a Context that is not an Activity or Service. Try passing a valid activity reference
** u can use application context to create dialog by adding before call to Dialog.show();
Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- but thisrequires permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Ref:
Solution 2:
You can't use an application [or service] context. If you really want to show you diallog from an application, you will have to pass an Activity context to it. You could also store the Activity context, but I do not recommend that. The activity context is voided on finish, so you will break your program. As @LeoLink said, just call it directly from your Activity.
EDIT For Example
classMyDialog {
public Dialog show(Context context) {
Dialogd=newDialog(context);
d.setTitle("I'm a dialog");
d.setMessage("I'm a message");
return d.show();
}
}
Solution 3:
The Application class is there to hold data that can be used by your activities, not to interact with the UI. Display the dialog from the activity you want it displayed in.
EDIT: If you want to call the code from multiple activities, you can have a superclass for these activities, that contain the code with the dialog. Then extend this superclass in all the activities you want to display the dialog, and call it from there.
Post a Comment for "How To Show Dialog Box From The Class Which Extends Application In Android?"