Skip to content

Instantly share code, notes, and snippets.

@Atternatt
Last active August 30, 2018 17:08
Show Gist options
  • Save Atternatt/33ddd8bf84eeafb7a324a34d8a9c340d to your computer and use it in GitHub Desktop.
Save Atternatt/33ddd8bf84eeafb7a324a34d8a9c340d to your computer and use it in GitHub Desktop.
Bundle Delegate
class UsageFragment : Fragment() {
private val numberWhatever by Argument("ARG_PROPERTY", 0)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//same behaviour tham BundleParam but withArguments in fragments
print("$numberWhatever!")
}
}
class UsageActivity : Activity() {
private val numberWhatever by BundleParam("EXTRA_PROPERTY", 0)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//here we will get the property from the activity intent thanks to delegating it
print("$numberWhatever!")
}
}
@Singleton
class UsagePrefs(context: Context) {
//we can observe changes of properties to update others
var userName: String by PrefParam(context = context, default = "") { lastUpdate = today.time}
private var lastUpdate: Long by PrefParam(context = context, default = 0L)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment