androidbroadcast / ViewBindingPropertyDelegate

Make work with Android View Binding simpler
https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719
Apache License 2.0
1.43k stars 102 forks source link

ViewBindingProperty can't be shared between classes #16

Closed yoobi closed 4 years ago

yoobi commented 4 years ago

Hello, I've made the update to 1.2.0 but since then when I switch from a fragment to another fragment I get this error I'm using databinding (not viewbinding) however I've enabled it in my gradle as in the README.

Here is my gradle:

buildFeatures {
        dataBinding = true
        viewBinding = true
    }

dependencies {
    implementation 'com.kirich1409.viewbindingpropertydelegate:viewbindingpropertydelegate:1.2.0'
    // To use only without reflection variants of viewBinding
    implementation 'com.kirich1409.viewbindingpropertydelegate:vbpd-noreflection:1.2.0'
}
java.lang.IllegalStateException: Instance of ViewBindingProperty can't be shared between classes
        at by.kirich1409.viewbindingdelegate.ViewBindingProperty.getValue(ViewBindingProperty.kt:29)
        at com.application.poc.ui.search.SearchFragment.getBinding(Unknown Source:7)
        at com.application.poc.ui.search.SearchFragment.onDestroyView(SearchFragment.kt:103)
        at androidx.fragment.app.Fragment.performDestroyView(Fragment.java:2909)
        at androidx.fragment.app.FragmentManager.destroyFragmentView(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.removeCancellationSignal(FragmentManager.java:742)
        at androidx.fragment.app.FragmentManager$2.onComplete(FragmentManager.java:370)
        at androidx.fragment.app.FragmentAnim$2$1.run(FragmentAnim.java:175)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Here is the code:

class SearchFragment: Fragment(R.layout.fragment_search) {

    private val binding: FragmentSearchBinding by viewBinding(FragmentSearchBinding::bind)
    private lateinit var searchAdapter: SearchAdapter
    private val viewModel: SearchViewModel by lazy {
        ViewModelProvider(this).get(SearchViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        searchAdapter = SearchAdapter()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setHasOptionsMenu(true)
        binding.lifecycleOwner = this
        binding.rvSearchHistory.adapter = searchAdapter

        viewModel.termList.observe(viewLifecycleOwner, {
            it?.let {
                if(it is Resource.Success) searchAdapter.submitList(it.data.map { searchEntity ->
                    searchEntity.searchText
                })
            }
        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.rvSearchHistory.adapter = null
    }
}
kirich1409 commented 4 years ago

What version of Kotlin do you use?

yoobi commented 4 years ago

I'm currently using Kotlin 1.4.10

kirich1409 commented 4 years ago

The issue will be fixed in 1.2.1. The release will be available soon

yoobi commented 4 years ago

Sure thanks a lot for your work and reactivity :)