ApplicationProvider

Introduction: Retrieve the android application and the current activity from anywhere
More: Author   ReportBugs   
Tags:

Retrieve the android application from anywhere

Useful to develop a standalone library

//from anywhere
val application = ApplicationProvider.application

Retrieve the current activity from anywhere

//from anywhere
val currentActivity = ActivityProvider.currentActivity

Download

Buy Me a Coffee at ko-fi.com

Download

dependencies {
    implementation 'com.github.florent37:applicationprovider:(lastest version)'
}

Initialize classes that needs a context

You do not need to override the Application now

Before

class MyApplication : Application() {
    override fun onCreate(){
        Stetho.initializeWithDefaults(application)
    }
}

After

Using a provider (for libraries)

Note that you can include it directly on your library's aar

class StethoInitializer : ProviderInitializer() {
    override fun initialize(): (Application) -> Unit = {
        Stetho.initializeWithDefaults(application)
    }
}
<provider
     android:name=".timber.TimberInitializer"
     android:authorities="${applicationId}.StethoInitializer" />

Using an initializer

val InitializeStetho by lazy {
    ApplicationProvider.listen { application ->
        Stetho.initializeWithDefaults(application)
    }
}

class MainActivity : AppCompatActivity() {

    init {
        InitializeStetho
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    ...
    }
}
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea