Skip to content

Instantly share code, notes, and snippets.

@alahammad
Forked from rakshakhegde/fragment_utility.kt
Created June 20, 2018 11:26
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 alahammad/691482a1345c6ff7a36d1ba5ce83efcc to your computer and use it in GitHub Desktop.
Save alahammad/691482a1345c6ff7a36d1ba5ce83efcc to your computer and use it in GitHub Desktop.
Handy Idiom: Pass Arguments to Android Fragment using Kotlin + Anko
import android.support.v4.app.Fragment
import org.jetbrains.anko.bundleOf
/**
* Pass arguments to a Fragment without the hassle of
* creating a static newInstance() method for every Fragment.
*
* Declared outside any class to have full access in any
* part of your package.
*
* Usage: instanceOf<MyFragment>("foo" to true, "bar" to 0)
*
* @return Returns an instance of Fragment as the specified generic type with the params applied as arguments
*/
inline fun <reified T : Fragment> instanceOf(vararg params: Pair<String, Any>)
= T::class.java.newInstance().apply {
arguments = bundleOf(*params)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment