数据绑定-onActivityResult之后XML不更新 [英] Data binding - XML not updating after onActivityResult

查看:84
本文介绍了数据绑定-onActivityResult之后XML不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

activity onActivityResult(..)中更新 ObservableField ,但不将该值反映到 XML 中.如果我调用executePendingBindings(),它将起作用.但是问题是,有很多 ObservableField ,对于每个变量,我都需要调用 executePendingBindings().

Updating ObservableField inside onActivityResult(..) of activity but not reflecting that value to XML. If I call executePendingBindings() it will work. But the issue is, there are a lot of ObservableField and for every variable, I need to call executePendingBindings().

有人知道如何解决这个问题吗?

Anyone knows how to solve this issue ?

推荐答案

ViewDataBinding.java 文件中,有一种请求重新绑定的方法

In ViewDataBinding.java file, there is one method for requesting rebind

protected void requestRebind() {
        if (mContainingBinding != null) {
            mContainingBinding.requestRebind();
        } else {
            synchronized (this) {
                if (mPendingRebind) {
                    return;
                }
                mPendingRebind = true;
            }
            if (mLifecycleOwner != null) {
                Lifecycle.State state = mLifecycleOwner.getLifecycle().getCurrentState();
                if (!state.isAtLeast(Lifecycle.State.STARTED)) {
                    return; // wait until lifecycle owner is started
                }
            }
            if (USE_CHOREOGRAPHER) {
                mChoreographer.postFrameCallback(mFrameCallback);
            } else {
                mUIThreadHandler.post(mRebindRunnable);
            }
        }
    }

当控件进入该方法时, mPendingRebind 将为false,并且此方法将其设为true. mChoreographer.postFrameCallback(mFrameCallback); 将再次将该变量设置为false,以便其他绑定将更新视图.

When control enters that method mPendingRebind will be false and it will be made true by this method. mChoreographer.postFrameCallback(mFrameCallback); will make that variable again to false so that other bindings will update the view.

问题出在!state.isAtLeast(Lifecycle.State.STARTED)条件.由于ObservableField是从 onActivityResult(..)更新的,因此当前状态将是 Lifecycle.State.CREATED ,因此该条件将失败并返回该方法而无需调用 mChoreographer.postFrameCallback(mFrameCallback); mPendingRebind 为true.因此,剩余的绑定不会更新.

The issue was !state.isAtLeast(Lifecycle.State.STARTED) condition. Since ObservableField is updated from onActivityResult(..), the current state will be Lifecycle.State.CREATED So that condition will fail and return that method without calling mChoreographer.postFrameCallback(mFrameCallback); and mPendingRebind will be true. So remaining bindings will not update.

因此解决方案是调用 SingleLiveEvent 并观察该变量.收到该回调后,我们必须更新ObservableField()

So the solution was to call SingleLiveEvent and observe that variable. When that callback is received, we have to update ObservableField()

例如:

val updateObserver = SingleLiveEvent<Unit>()

然后观察该事件

updateObserver.observe(this, Observer { 
            //update ObservableField
        })

从onActivityResult调用该事件

Call that event from onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 111 && resultCode == Activity.RESULT_OK) {
               updateObserver.call()
            }
    }

这篇关于数据绑定-onActivityResult之后XML不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆