建立UI实用程序类的最佳方法 [英] Best way to build an UI utility class

查看:100
本文介绍了建立UI实用程序类的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个实用程序类.它应该从提供典型UI或交互功能的数据库中读取值,例如设置应用程序的标题,设置背景图片,播放背景音乐.

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.

由于提出了其他问题如何在非UI方法中使用UI函数?我意识到我有很多不同的方法可以做到这一点.我测试了各种方法来设定每一个工作的标题.但这是最干净/正确/自信的方法,为什么?

Because of asking a other question 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?

在示例中,调用类是kotlin类,但这无关紧要.

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.

通话类:

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

叫课:

  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();

    }

第二种.隐藏尽可能.一切都必须传递给实用程序类.更多活动使用时如何处理.在utilitie类中使用静态Activity声明仍然可行吗?

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?

通话班:

    frameworkFeatures(applicationContext,this)
    frameworkFeatures.frameworkSetTitleEverythingisStatic()

叫课:

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());

}

推荐答案

如果您需要有效的上下文/活动,则可以提供初始化API,该API应该在Application onCreate()中调用.像这样的东西:

If you need to have valid context/activity you could instead provide initialize API that should be called in Application onCreate(). Something like :

Framework.initialize(appContext); 

您可以在其中注册自己的Application.ActivityLifecycleCallbacks来跟踪当前可见/活动:

Inside of which you could register your own Application.ActivityLifecycleCallbacks that would track currently visible/active activity:

public static void initialize(Application context) {
    appContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
       ...
       @Override
       public void onActivityResumed(Activity activity) {
           sGlobalContext = activity; 
       }
       @Override
       public void onActivityPaused(Activity activity) {
           sGlobalContext = null; 
       }
       ....
    ...
}

然后您的框架就不必再担心活动/上下文了.对于用户,所有内容都是透明的.

And then your framework shouldn't worry about activity/context anymore. For User all will be transparent.

这篇关于建立UI实用程序类的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆