Skip to content
Retrieve the android application and the current activity from anywhere
Kotlin Java Shell
Branch: master
Clone or download
florent champigny
florent champigny published
Latest commit e42b3a2 Nov 30, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea initial import Nov 28, 2018
app added jcenter Nov 30, 2018
applicationprovider added jcenter Nov 30, 2018
gradle added jcenter Nov 30, 2018
.gitignore initial import Nov 28, 2018
README.md published Nov 30, 2018
build.gradle added jcenter Nov 30, 2018
gradle.properties initial import Nov 28, 2018
gradlew initial import Nov 28, 2018
gradlew.bat initial import Nov 28, 2018
publish.sh added jcenter Nov 30, 2018
settings.gradle initial import Nov 28, 2018

README.md

ApplicationProvider

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?) {
    ...
    }
}
You can’t perform that action at this time.