Skip to content

Instantly share code, notes, and snippets.

@johnjohndoe
Created February 19, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnjohndoe/2314caa406a05cb87f67ebf81155dcd5 to your computer and use it in GitHub Desktop.
Save johnjohndoe/2314caa406a05cb87f67ebf81155dcd5 to your computer and use it in GitHub Desktop.
Instance of a LifecycleObserver (MyClass) which registers itself to observe lifecycle events.
inline fun <reified T : MyClass> android.arch.lifecycle.LifecycleOwner.createMyClassInstance(parameter: String): T {
val instance = T::class.constructors.first { it.parameters.isNotEmpty() }.call(parameter)
// Don't forget to invoke removeObserver(instance)!
lifecycle.addObserver(instance)
return instance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment