• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Best way to build an UI utility class

I want to build an utility class. It should read values from an database an providing typical UI or interaction features e.g. setting the title of the application, setting a background picture, playing background music.

Because of asking a other question at Stackoverflow How to use UI functions in non UI methods? I realized that i have a bunch of different ways to do that. I tested different ways for setting the titel every, every way worked. But which is the cleanest / correct / confident way, and why?

In the examples the calling class is a kotlin class, but this is indifferent.

Way one! Everything Activity related is done in the calling activity. Context is passed through constructor, could alternatively invoked with every function call. In my opinion we do have here not so much dependencies but the utility user has to do a lot himself.

calling class:

Code:
frameworkFeatures(applicationContext);
setTitle(frameworkFeatures.frameworkSetTitlesStaticContext());

called class:

Code:
  private static Context frameworkContext;

   public frameworkFeatures(Context context) {

       frameworkContext = context;

   }

  public static String frameworkSetTitlesStaticContext(){

       TestMainDatabase.getDatabase(frameworkContext);
       TestMainDatabase db = RoomAsset.databaseBuilder(frameworkContext, TestMainDatabase.class, "TestMainDatabase.db").allowMainThreadQueries().build();
       return db.featuresDao().findByName("title").getFeatureValue().toString();

   }

Way two. Hiding as much as possible. Everything has to be passed to utility class. How to handle this when used by more Activity. Would it still be possible with a static Activity declaration in the utilitie class?

calling class:

Code:
   frameworkFeatures(applicationContext,this)
   frameworkFeatures.frameworkSetTitleEverythingisStatic()

called class:

Code:
private static Context frameworkContext;
private static Activity frameworkActivity;
private static TestMainDatabase frameworkTestMainDatabase;

public frameworkFeatures(Context context, Activity activity){

   frameworkContext = context;
   frameworkActivity = activity;

   TestMainDatabase.getDatabase(context);
   frameworkTestMainDatabase = RoomAsset.databaseBuilder(frameworkContext,TestMainDatabase.class, "TestMainDatabase.db").allowMainThreadQueries().build();


}

public static void frameworkSetTitleEverythingisStatic(){

   frameworkActivity.setTitle(frameworkTestMainDatabase.featuresDao().findByName("title").getFeatureValue().toString());

}
 
Last edited by a moderator:
Why are you declaring a static method? It's not necessary to create an instance of a class to invoke a static method anyway.
Bad idea to use static member variables because as pointed out in the SO question, it can cause memory leaks.
 
Just as a follow up to this, the normal design pattern to use here would the database helper class (example below). Your helper class should not really be concerned with updating UI components, but should return the correct data that allows the calling Activity to update its components.
What I do in addition, is to make my helper class a singleton, so I don't have to create a new instance every time I wish to use it.

https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
 
Back
Top Bottom