Newer
Older
MonCalc / app / src / main / kotlin / jackall / moncalc / App.kt
package jackall.moncalc

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle
import io.realm.Realm

/**
 * Created by matsumoto_k on 2017/11/01.
 */
class App : Application() {
    var currentActivity: Activity? = null

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var instance: App
            private set
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        Realm.init(this)
        registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, saveInstanceState: Bundle?) {

            }

            override fun onActivityStarted(activity: Activity) {
                currentActivity = activity
            }

            override fun onActivityResumed(activity: Activity) {

            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {
                if (currentActivity === activity) {
                    currentActivity = null
                }
            }

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

            }

            override fun onActivityDestroyed(activity: Activity) {

            }
        })
    }
}