GithubHelp home page GithubHelp logo

numeroanddev / viewbinding-ktx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wada811/viewbinding-ktx

0.0 1.0 0.0 137 KB

ViewBinding-ktx make easy declaring ViewBinding.

Home Page: https://medium.com/databinding-ktx

License: Apache License 2.0

Kotlin 100.00%

viewbinding-ktx's Introduction

ViewBinding-ktx

ViewBinding-ktx make easy declaring ViewBinding.

DataBinding-ktx is here.

Problems in ViewBinding

  • Differences in the way of declaring a binding variable in Activity and Fragment.
    • In Activity, you can declare the binding variable using by lazy.
    • In Fragment, you can't declare the binding variable using by lazy because of recreating Fragment's view.
  • Wasted memory unless you set the binding variable to null after onDestroyView
    • If you use Navigation component, BackStack or detach in Fragment, Fragment is still alive but Fragment's view is dead after onDestroyView. Because the binding variable has view tree, your app wasted memory. For saving memory, you should set the binding variable to null after onDestroyView.

Overview

ViewBinding-ktx

  • provides the unified way of declaring the binding variable
    • between Activity and Fragment.
    • between DataBinding-ktx and ViewBinding-ktx.
  • is saving memory
    • The binding variable is stored as the Fragment's view's tag.
    • The binding variable is cleaning up along with the Fragment's view after onDestroyView.
  • needs one of the following
    • calling setContentView in Activity and calling inflater.inflate in onCreateView of Fragment.
    • calling Activity/Fragment's secondary constructor passing layout res id.

Usage

Reflection

private val binding: ViewBindingActivityBinding by viewBinding()

No reflection

private val binding by viewBinding { ViewBindingActivityBinding.bind(it) }

Activity

  • Use Activity's secondary constructor passing layout res id.
class ViewBindingActivity : AppCompatActivity(R.layout.view_binding_activity) {
    // Declare the `binding` variable using `by viewBinding()`.
    private val binding: ViewBindingActivityBinding by viewBinding()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // You can use binding
    }
}

Fragment

  • Use Fragment's secondary constructor passing layout res id.
class ViewBindingFragment : Fragment(R.layout.view_binding_fragment) {
    // Declare the `binding` variable using `by viewBinding()`.
    private val binding: ViewBindingFragmentBinding by viewBinding()
    // DO NOT override onCreateView
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // You can use binding
    }
}

Gradle

android {
    buildFeatures {
        viewBinding = true
    }
}

repositories {
    maven { url "https://www.jitpack.io" }
}

dependencies {
    implementation 'com.github.wada811:ViewBinding-ktx:x.y.z'
}

License

Copyright (C) 2020 wada811

Licensed under the Apache License, Version 2.0

viewbinding-ktx's People

Contributors

wada811 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.