Big brother, a small and beautiful Application component, let's take a look

Introduction

In the Android development process, the role of the Application class cannot be ignored. It is not only the entry point for program startup, but also represents the life cycle of the entire application. In Application, we usually do the following:

  • Initialize various third-party libraries
  • Register ActivityLifecycleCallbacks to monitor the front and back status of the application
  • Provide a global Application instance for use in subsequent code
  • Implement other global methods

The need to initialize third-party libraries is easy to understand, because some third-party libraries require the global Application object to perform certain operations. This requires us to follow the initialization code provided by the third-party library, although this process is a bit cumbersome.
If you have some open source components yourself, or your company's internal components also need the Application object or Context, then you can agree on a rule to let everyone use the same Application instance. In this way, it only needs to be initialized once in the project, and other components do not need to be initialized each.

There are also some general Application-related methods, such as monitoring the front and back status of the application, judging whether it is the main process, etc., can be integrated in a component, and can be used directly depending on this component, without having to write repeatedly in each application Same logic.

application widget

Based on the above thinking, a lightweight and efficient Application component came into being. The function of this component is clear and straightforward:

  • Easy to use and non-intrusive
  • Provides common application status monitoring and global ApplicationContext

Component github address: https://github.com/yuzhiqiang1993/application

How to use

add dependencies

implementation("com.xeonyu:application:1.0.0")

Initialize in your Application class

AppManager.init(this)

Global Application context

Note the package name:

import com.yzq.application.AppContext
//示例 ApplicationContext
AppContext.checkSelfPermission( android.Manifest.permission.WRITE_EXTERNAL_STORAGE)

//示例 Application
Utils.init(AppManager.application)

Application Status Monitoring

AppManager.addAppStateListener(object : AppStateListener {
    
    

    override fun onAppForeground() {
    
    
        /*App切换到前台时的操作*/
    }

    override fun onAppExit() {
    
    
        /*App退出时的操作*/
    }

    override fun onAppBackground() {
    
    
        /*App切换到后台时的操作*/
    }
})

Other common functions

/*获取当前栈顶的Activity*/
val topActivity = AppManager.topActivity
/*判断是否是主进程*/
val mainProcess = AppManager.isMainProcess()
/*判断是否处于前台*/
val foreground = AppManager.isForeground
/*退出应用程序*/
AppManager.exitApp()

If your other components need global Application information, you can directly rely on this component to use it, eliminating the need to pass the Application to the user.

Well, that's it for this article.


If you think this article is helpful to you, please give it a thumbs up. It can help more developers. If there are any mistakes in the article, please correct me. For reprinting, please indicate that you are reposting from Yu Zhiqiang’s blog, thank you !

Guess you like

Origin blog.csdn.net/yuzhiqiang_1993/article/details/131654606