Is there a convention for helper class in Android?

For every action that I add to my application, I notice a lot of similar code used to initialize the Activity. A helper class with a static method to wrap this similar code seems capable.

At first I thought of a singleton class. I can add static methods / variables and use them in the application. I really did not try to understand how this works in an Android application. Looking for a little more, I saw something about creating a class that extends Application . For this, I made a simple test:

 public class MyApp extends Application { public static String DEMOTEXT = "WORKING!"; public static void ShowToast(Context context, String text) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } } MyApp.ShowToast(this, MyApp.DEMOTEXT); // Placed on onCreate of some Activity 

This works exactly as I expected. Is this a way to go for Android or is there a better deal? What else should I consider?

By the way, should the final keyword be used in a string? What about the method?

EDIT: I just read this:

Typically, there is no need for a subclass of Application. In most cases, static singlets can provide the same functionality in a more modular way. If your singleton needs a global context (for example, to register broadcast receivers), a function to get it can be provided with a Context that internally uses Context.getApplicationContext () when we first build a singleton.

http://developer.android.com/reference/android/app/Application.html

Should I use singleton then?

+4
source share
3 answers

Application is mainly used to globally initialize an application. You must create your own class, override Application.onCreate () and initialize your application static data there.

Remember to declare it in AndroidMainfest.xml:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="your.package.path.to.MyApp"> 

A static helper class is created the way you did.
The convention is to use a lowercase letter in the first position, so MyApp.showToast(...) .

You would use final for String if you wanted to avoid ailments elsewhere (since it should be contant).

 // this would allow ... public static String DEMOTEXT = "WORKING!"; // ... to do this somewhere else MyApp.DEMOTEXT = "NOT WORKING!" 
+3
source

I have not tried this, but I think that you can do something similar too.

 public class MyActivity extends Activity { private static final String DEMOTEXT = "WORKING!"; @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); Toast.makeText(this, DEMOTEXT, Toast.LENGTH_SHORT).show(); } } 

Now for all the actions that should use this initialization, you can simply expand your base activity class.

 public class SomeActivity extends MyActivity { ... // Should display the toast on create ... } 
+2
source

Yes, just use singleton. In this case, if your methods are static, you don't even need a singleton. Just a class with static methods.

+1
source

All Articles