组件化Application共享方案

前言

在日常开发中,随着项目功能越来越多,业务也越来越复杂,普通的项目结构也就不适合团队协作,分享一种项目组件化后全局获取应用上下文的方案。


1.定义Application接口,供Module实现
interface IModuleInit {
    fun onCreate() {}
    fun attachBaseContext(base: Context) {}
    fun onLowMemory() {}
    fun onTrimMemory(level: Int) {}
    fun onTerminate() {}
}
2.Module实现类
abstract class BaseModuleInit: IModuleInit {
    /**
     * 模块初始化优先级 越高初始化越快
     * Module中设置优先级越小越优先初始化
     */
    open val priority: Int = 0
}
3.Module代理类
object ModuleInitDelegate : IModuleInit {

    private val moduleList = mutableListOf<IModuleInit>()

    fun register(vararg modules: IModuleInit) {
        moduleList.addAll(modules)
    }

    fun reorder() {
        moduleList.sortBy { (it as BaseModuleInit).priority }
    }

    override fun onCreate() {
        moduleList.forEach { it.onCreate() }
    }

    override fun attachBaseContext(base: Context) {
        moduleList.forEach { it.attachBaseContext(base) }
    }

    override fun onLowMemory() {
        moduleList.forEach { it.onLowMemory() }
    }

    override fun onTrimMemory(level: Int) {
        moduleList.forEach { it.onTrimMemory(level) }
    }

    override fun onTerminate() {
        moduleList.forEach { it.onTerminate() }
    }
}
4.让Application继承自ApplicationProvider
open class ApplicationProvider : Application() {

    companion object {
        // 全局共享的 Application
        lateinit var appContext: Application
    }

    override fun onCreate() {
        super.onCreate()
        appContext = this
        ModuleInitDelegate.reorder()
        ModuleInitDelegate.onCreate()
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        ModuleInitDelegate.attachBaseContext(base)
    }

    override fun onLowMemory() {
        super.onLowMemory()
        ModuleInitDelegate.onLowMemory()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        ModuleInitDelegate.onTrimMemory(level)
    }

    override fun onTerminate() {
        super.onTerminate()
        ModuleInitDelegate.onTerminate()
    }
}
5.在组件中定义ModuleInit类,继承自BaseModuleInit类,做初始化相关
class ModuleInit: BaseModuleInit() {

    override fun onCreate() {
        val context = ApplicationProvider.appContext
        ImageLoader.getDefault().diskCacheOptions()
            .setDiskCacheDirPath(context.getExternalFilesDir("cache")?.path ?: context.filesDir.path)
            .setDiskCacheFolderName("image")
            .setDiskCacheSize(2 * 1024 * 1024) // 设置磁盘缓存2G
            .setBitmapPoolSize(2.0f)
            .setMemoryCacheSize(1.5f)
            .build()
    }

    override fun onLowMemory() {
        super.onLowMemory()
        ImageLoader.getDefault().clearMemory(ApplicationProvider.appContext)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        ImageLoader.getDefault().trimMemory(ApplicationProvider.appContext, level)
    }
}
6.在Application中注册
class AppContext : ApplicationProvider() {

    init {
        ModuleInitDelegate.register(ModuleInit(), ModuleInit1(), ModuleInit2())
    }
}
7.应用上下文获取
  • 使用共享的Application
ApplicationProvider.appContext
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒙同學

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值