Last active
January 21, 2019 07:16
-
-
Save iamnaran/0c84128d073872f7d3e5335e108afe5a to your computer and use it in GitHub Desktop.
Application Class in Kotlin #android #application #singleton #class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.app.Application | |
import android.content.Context | |
import android.content.SharedPreferences | |
import android.os.Bundle | |
import android.preference.PreferenceManager | |
import android.util.Log | |
class Application : Application(), Application.ActivityLifecycleCallbacks { | |
companion object { | |
private var instance: Application? = null | |
private var sharedPreferences: SharedPreferences? = null | |
private var isActive: Boolean = false | |
fun applicationContext(): Context { | |
return instance!!.applicationContext | |
} | |
fun getSharedPreference(): SharedPreferences? { | |
return sharedPreferences | |
} | |
fun isActivityVisible(): Boolean { | |
return isActive | |
} | |
} | |
override fun onCreate() { | |
super.onCreate() | |
// initialize for any | |
// Use ApplicationContext. | |
// example: SharedPreferences etc... | |
instance = this | |
val context: Context = Application.applicationContext() | |
registerActivityLifecycleCallbacks(this) | |
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) | |
} | |
override fun onActivityPaused(activity: Activity?) { | |
isActive = false | |
} | |
override fun onActivityResumed(activity: Activity?) { | |
isActive = true | |
} | |
override fun onActivityStarted(activity: Activity?) { | |
} | |
override fun onActivityDestroyed(activity: Activity?) { | |
} | |
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) { | |
} | |
override fun onActivityStopped(activity: Activity?) { | |
} | |
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment