package com.example.base

import android.app.Activity
import android.os.Bundle
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.multidex.MultiDexApplication
import com.example.base.global.ActivityGlobalManager


/**
 * @date 2021-08-13 09:05
 * @desc 程序入口
 */

open class MvvmApplication : MultiDexApplication(), ViewModelStoreOwner {

    // 通过 Application 创建全局共享的 VM
    override val viewModelStore = ViewModelStore()

    override fun onCreate() {
        super.onCreate()
        //注册自己的Activity的生命周期回调接口。
        registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
    }

    private val activityLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityPaused(activity: Activity) {
        }

        override fun onActivityStarted(activity: Activity) {
        }

        override fun onActivityDestroyed(activity: Activity) {
            ActivityGlobalManager.removeActivity(activity)
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        }

        override fun onActivityStopped(activity: Activity) {
        }

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            ActivityGlobalManager.addActivity(activity)
        }

        override fun onActivityResumed(activity: Activity) {
        }

    }
}