Skip to content

Android library written in Kotlin to simplify passing params to activities and fragments.

License

Notifications You must be signed in to change notification settings

cregus/android-params

Repository files navigation

Android Params

Android library to simplify passing params to activities and fragments (currently it's working only with the androidx fragments).

Setup

Add the JitPack repository to your build file

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add library dependency to your project build file

dependencies {
  implementation 'com.github.cregus:android-params:1.1'
}

Sample usage

Passing params to activity

class UserActivity : AppCompatActivity() {
    companion object : ActivityParams<UserActivity, Long> by activityParams()

    private val id by loadParams()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Log.d("Params", "id: $id")
    }
}

and then:

val intent = UserActivity.createIntent(context, 1)

Passing params to fragment with default value

class UserFragment : Fragment() {
    companion object : FragmentParams<UserFragment, Long> by fragmentParams()

    private val id by loadParams(0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Log.d("Params", "id: $id")
    }
}

and then:

val fragment = UserFragment.newInstance(1)

Passing multiple params

class UserFragment : Fragment() {
    @Parcelize
    data class Params(val id: Long, val name: String) : Parcelable

    companion object : FragmentParams<UserFragment, Params> by fragmentParams()

    private val params by loadParams()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Log.d("Params", "id: ${params.id}, name: ${params.name}")
    }
}

and then:

val fragment = UserFragment.newInstance(UserFragment.Params(1, "John Smith"))

About

Android library written in Kotlin to simplify passing params to activities and fragments.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages