Can I create my own global methods in my class of Android applications?

I currently have an application that has many actions and should have a way to maintain state between these actions.

I use the Application class to do this, declaring my global variables and using getters and setters to interact with my actions.

I was hoping to post some custom methods there, so when I want to perform a general task, for example, display an error message, I can declare a method in my application class and call it from any activity that uses it

EscarApplication application = (EscarApplication) this.getApplication(); 

EscarApplication is the name of my application class above.

I tried to include this method in my application class:

 public void showError(String title, String message) { Log.i("Application level",message); this.alertDialog.setTitle(title); alertDialog.setMessage(message); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { return; } }); alertDialog.show(); } 

In the hope that I can call this method from activity without having to reuse it, but when I call it using something like below, I get an exception from the null pointer:

 Visit.this.application.showError("Update error", "An error has occurred while trying to communicate with the server"); 

Visit the name of my current action above.

If this works, or I can only use getters and setters to change global var in the application class.

Stack trace:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{escar.beedge/escar.beedge.HomeScreen}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) at android.app.ActivityThread.access$2100(ActivityThread.java:116) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4203) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) at dalvik.system.NativeStart.main(Native Method) ERROR/AndroidRuntime(375): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRoot.setView(ViewRoot.java:460) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) at android.app.Dialog.show(Dialog.java:238) at escar.beedge.EscarApplication.showError(EscarApplication.java:98) at escar.beedge.HomeScreen.onCreate(HomeScreen.java:30) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 

The dialog is declared as such in the application class:

 AlertDialog alertDialog; 

Created in the same class:

 alertDialog = new AlertDialog.Builder(this).create(); 

and a method to call this class in the following class:

 public void showError(String title, String message) { alertDialog.setTitle(title); alertDialog.setMessage(message); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { return; } }); alertDialog.show(); } 

And finally, it is called from such activity:

 EscarApplication application; application = (EscarApplication) this.getApplication(); application.showError("test", "display this message"); 
+4
source share
4 answers

If you need to maintain state between actions, use the service. Everything else is hacking

+1
source

Someone will correct me if I am wrong, but the application class will not be able to execute the objects associated with the view, since they must be bound to the view, which should be associated with the action.

In this sense, you can use your Application class to implement a static method that configures the dialog.

 public static void setDialog(String title, String message,AlertDialog alertDialog){ alertDialog.setTitle(title); alertDialog.setMessage(message); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { return; } }); } 

but you will need to create a dialog and call the show method for the actions themselves (in fact, maybe even a button which should be installed in the dialog box) must be created in action)

Another option would be to implement the extends class of the AlertDialog class and whose button is pre-configured for the desired behavior.

0
source

You can use the Singleton template.

-1
source

I am looking to achieve something similar to you.

I did not find an official answer, but it seems that you should not use the application context for Toast and Dialogs. Instead, try using the Activity context as follows:

 // From inside your activity Dialog dialog = new Dialog(this); 

instead of this:

 // From inside your Application instance Dialog dialog = new Dialog(getApplicationContext()); 

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

-1
source

All Articles