Skip to content Skip to sidebar Skip to footer

Can I Create Custom Global Methods In My Android Application Class?

I currently have an app that has many activities and needs to have a way of maintaining state between these activities. I use the Application class to do this, declaring my global

Solution 1:

If you need to maintain state between activities, then use a service. Anything else is a hack

Solution 2:

Someone correct me if Im wrong, but an Application class wont be able to execute view related objects as they need to be bound to a view which needs to be bound to an activity.

In that sense, you could use your Application class to implement a static method that customises the dialog

publicstaticvoidsetDialog(String title, String message,AlertDialog alertDialog){
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setButton("OK", newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog,int which) {
                return;
            }
        });
}

but you would have to create the dialog and call the show method on the activities themselves (actually maybe even the button to be set in the dialog would need to be created on the activity)

Another option could be to implement a class that extends the AlertDialog class and whose button is pre-set to the behavior you want.

Solution 3:

Solution 4:

I'm looking to achieve something similar to you.

I haven't found an official answer, but it looks like you shouldn't be using the application context for Toast and Dialogs. Instead, try using the context of an Activity like so :

// From inside your activityDialogdialog=newDialog(this);

instead of this:

// From inside your Application instanceDialogdialog=newDialog(getApplicationContext());

Read this : Android: ProgressDialog.show() crashes with getApplicationContext

Post a Comment for "Can I Create Custom Global Methods In My Android Application Class?"