wada811 / ViewLifecycleProperty

ViewLifecycleProperty makes easy declaring the property that has the same lifecycle as Fragment's view.

Home Page:https://medium.com/viewlifecycleproperty

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ViewLifecycleProperty

ViewLifecycleProperty makes easy declaring the property that has the same lifecycle as Fragment's view.

Overview

ViewLifecycleProperty is

  • lazy initialization
  • accessible when Fragment's view isn't null i.e., between onCreateView and onDestroyView
  • automatically cleared by null when Fragment's view released i.e., after onDestroyView

Usage

val by lazy style

class SampleFragment : Fragment(R.layout.sample_fragment) {
    private val property: SampleProperty by viewLifecycle { SampleProperty(viewLifecycleOwner) }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // You can use property
    }
    override fun onDestroyView() {
        super.onDestroyView()
        // You can use property unlike AutoClearedValue
    }
}

AutoClearedValue style

class SampleFragment : Fragment(R.layout.sample_fragment) {
    private var property: SampleProperty by viewLifecycle()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // You must initialize before using property
        property = SampleProperty(viewLifecycleOwner)
        // You can use property
    }
    override fun onDestroyView() {
        super.onDestroyView()
        // You can use property unlike AutoClearedValue
    }
}

Gradle

Maven Central

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.wada811.viewlifecycleproperty:viewlifecycleproperty:x.y.z'
}

License

Copyright (C) 2020 wada811

Licensed under the Apache License, Version 2.0

About

ViewLifecycleProperty makes easy declaring the property that has the same lifecycle as Fragment's view.

https://medium.com/viewlifecycleproperty

License:Apache License 2.0


Languages

Language:Kotlin 100.0%