Coder Social home page Coder Social logo

Access to viewBinding after Lifecycle is destroyed or hasn't created yet. The instance of viewBinding will be not cached. about viewbindingpropertydelegate HOT 10 CLOSED

Reginer avatar Reginer commented on August 15, 2024
Access to viewBinding after Lifecycle is destroyed or hasn't created yet. The instance of viewBinding will be not cached.

from viewbindingpropertydelegate.

Comments (10)

Reginer avatar Reginer commented on August 15, 2024 3

add ViewBindingPropertyDelegate.strictMode = false in Application#onCreate

Maybe the author forgot what he wrote .

from viewbindingpropertydelegate.

Reginer avatar Reginer commented on August 15, 2024 1

May be it's normal you've checked , But app creash . Why throw an exception? shouldn't do anything.

from viewbindingpropertydelegate.

kirich1409 avatar kirich1409 commented on August 15, 2024

@Reginer , I need a sample where the issue is reproducible

from viewbindingpropertydelegate.

Reginer avatar Reginer commented on August 15, 2024

After activity destroy ,binding.textView.setText

from viewbindingpropertydelegate.

kirich1409 avatar kirich1409 commented on August 15, 2024

I've checked the behavior and it's normal. After Lifecycle became DESTROYED viewbinding keep last creataed version.

from viewbindingpropertydelegate.

Reginer avatar Reginer commented on August 15, 2024

image

from viewbindingpropertydelegate.

Reginer avatar Reginer commented on August 15, 2024

Oh , strictMode default value should be false.

from viewbindingpropertydelegate.

kirich1409 avatar kirich1409 commented on August 15, 2024

It's not connected with StrictMode. It's part of normal flow now to work with ViewBinding when view isn't exist

from viewbindingpropertydelegate.

cdongieux avatar cdongieux commented on August 15, 2024

Hi @kirich1409

I have the same problem in my app:

class HomeActivity : FragmentActivity() {
    private val binding: ActivityHomeBinding by viewBinding()

    override fun onDestroy() {
        binding.list.adapter = null     // crash happens here
        super.onDestroy()
    }
}

With list being a RecyclerView.

And the stacktrace:

Caused by java.lang.IllegalStateException: Access to viewBinding after Lifecycle is destroyed or hasn't created yet. The instance of viewBinding will be not cached.
       at by.kirich1409.viewbindingdelegate.LifecycleViewBindingProperty.runStrictModeChecks(ViewBindingProperty.kt:112)
       at by.kirich1409.viewbindingdelegate.LifecycleViewBindingProperty.getValue(ViewBindingProperty.kt:91)
       at by.kirich1409.viewbindingdelegate.LifecycleViewBindingProperty.getValue(ViewBindingProperty.kt:71)
       at net.foobar.ui.home.HomeActivity.getBinding(HomeActivity.kt:31)
       at net.foobar.ui.home.HomeActivity.onDestroy(HomeActivity.kt:174)
       at android.app.Activity.performDestroy(Activity.java:8063)
       at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1341)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4926)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4970)
       at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7356)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

As the documentation in Lifecycle.State.DESTROYED says:

this state is reached right before Activity's onDestroy call.

So technically the Activity is not destroyed yet, and its views neither. So I think the runStrictModeChecks() in ViewBindingProperty.getValue() is wrong.

What do you think about it?

from viewbindingpropertydelegate.

kirich1409 avatar kirich1409 commented on August 15, 2024

@cdongieux , sadly, but it can be normal. Destroy in the VИЗВ associated with Lifecycle, not with Activity lifecycle callbacks. It's better to use

private val binding: ActivityHomeBinding by viewBinding(onViewDestroyed = {...})

from viewbindingpropertydelegate.

Related Issues (20)

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.