BaseApp.kt 2.12 KB
Newer Older
1
package com.ydl.ydlcommon.base
konghaorui committed
2 3 4 5

import android.app.Application
import android.content.Context
import android.support.multidex.MultiDex
6 7 8 9 10
import com.ydl.ydlcommon.base.config.GlobalConfig
import com.ydl.ydlcommon.base.config.IApp
import com.ydl.ydlcommon.base.delegate.AppDelegate
import com.ydl.ydlcommon.base.delegate.IAppLifecycles
import com.ydl.ydlcommon.utils.YDLPreconditions
徐健 committed
11
import io.flutter.view.FlutterMain
konghaorui committed
12 13 14 15 16

/**
 * Created by haorui on 2019-08-21 .
 * Des: 基础Application
 */
17
open class BaseApp : Application(), IApp {
konghaorui committed
18 19 20 21 22

    private var mAppDelegate: IAppLifecycles? = null

    companion object {

23
        lateinit var instance: BaseApp
konghaorui committed
24 25 26 27 28 29 30

        fun getApp(): Application {
            return instance
        }
    }

    /**
31
     * 这里会在 [BaseApp.onCreate] 之前被调用,可以做一些较早的初始化
konghaorui committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
     * 常用于 MultiDex 以及插件化框架的初始化
     *
     * @param base
     */
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        instance = this;
        MultiDex.install(this)
        if (mAppDelegate == null)
            this.mAppDelegate = AppDelegate(base)
        this.mAppDelegate!!.attachBaseContext(base)
    }

    override fun onCreate() {
        super.onCreate()
        if (mAppDelegate != null)
            this.mAppDelegate!!.onCreate(this)
徐健 committed
49 50 51 52


        //Flutter 初始化需要在主线程中执行
        FlutterMain.startInitialization(this)
konghaorui committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }

    /**
     * 在模拟环境中程序终止时会被调用
     */
    override fun onTerminate() {
        super.onTerminate()
        if (mAppDelegate != null)
            this.mAppDelegate!!.onTerminate(this)
    }

    override fun getGlobalConfig(): GlobalConfig {
        YDLPreconditions.checkNotNull(mAppDelegate, "%s cannot be null", AppDelegate::class.java.name)
        YDLPreconditions.checkState(
            mAppDelegate is IApp,
            "%s must be implements %s",
            mAppDelegate!!::class.java.name,
            IApp::class.java.name
        )

        return (mAppDelegate as IApp).getGlobalConfig()
    }
}