Skip to content

ContextActivityProvider

Static Badge

A singleton object that helps to maintain a weak reference to the current activity, avoiding memory leaks by preventing strong references to an Activity

Implementation

  • Initialize it in your MainActivity.kt class
class MainActivity : ComponentActivity() {

    @CallSuper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        // attach the activity
        ContextActivityProvider.setCurrentActivity(this)

        ...
    }

}

Usage

Use it in composable methods

@Composable
fun SomeComposable() {
    val activity = ContextActivityProvider.getCurrentActivity()
}