1. Are you ready for the Galaxy S20? Here is everything we know so far!

Best way to build an UI utility class

Discussion in 'Android Development' started by freeLearning, Aug 5, 2017.

  1. freeLearning

    freeLearning Lurker
    Thread Starter

    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 (Text):
    1.  
    2. frameworkFeatures(applicationContext);
    3. setTitle(frameworkFeatures.frameworkSetTitlesStaticContext());
    4.  
    called class:

    Code (Text):
    1.  
    2.   private static Context frameworkContext;
    3.  
    4.    public frameworkFeatures(Context context) {
    5.  
    6.        frameworkContext = context;
    7.  
    8.    }
    9.  
    10.   public static String frameworkSetTitlesStaticContext(){
    11.  
    12.        TestMainDatabase.getDatabase(frameworkContext);
    13.        TestMainDatabase db = RoomAsset.databaseBuilder(frameworkContext, TestMainDatabase.class, "TestMainDatabase.db").allowMainThreadQueries().build();
    14.        return db.featuresDao().findByName("title").getFeatureValue().toString();
    15.  
    16.    }
    17.  
    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 (Text):
    1.  
    2.    frameworkFeatures(applicationContext,this)
    3.    frameworkFeatures.frameworkSetTitleEverythingisStatic()
    4.  
    called class:

    Code (Text):
    1.  
    2. private static Context frameworkContext;
    3. private static Activity frameworkActivity;
    4. private static TestMainDatabase frameworkTestMainDatabase;
    5.  
    6. public frameworkFeatures(Context context, Activity activity){
    7.  
    8.    frameworkContext = context;
    9.    frameworkActivity = activity;
    10.  
    11.    TestMainDatabase.getDatabase(context);
    12.    frameworkTestMainDatabase = RoomAsset.databaseBuilder(frameworkContext,TestMainDatabase.class, "TestMainDatabase.db").allowMainThreadQueries().build();
    13.  
    14.  
    15. }
    16.  
    17. public static void frameworkSetTitleEverythingisStatic(){
    18.  
    19.    frameworkActivity.setTitle(frameworkTestMainDatabase.featuresDao().findByName("title").getFeatureValue().toString());
    20.  
    21. }
    22.  
     


    #1 freeLearning, Aug 5, 2017
    Last edited by a moderator: Aug 5, 2017

    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    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.
     
  3. Deleted User

    Deleted User Guest

    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/
     
    GameTheory likes this.
  4. saurabhhooda

    saurabhhooda Lurker

Loading...

Share This Page

Loading...