在 Activity 的初始 OnResume 之前执行 DataBinding

Executing DataBinding Prior to Initial OnResume of Activity

我在我的应用程序中设置了与一些自定义属性的数据绑定。生成的 类 看起来是正确的,我什至可以看到默认值在 executeBindings 方法中绑定到生成的绑定 类 中的属性的位置。我需要在首次创建 Activity 的初始 OnResume 中访问这些自定义属性,并发现绑定在调用生命周期方法 AFTER 后执行。因此,当我最初检查时,该属性为空。显然,在 OnPause 之后的后续 OnResume 调用中,一切正常。有没有办法强制初始绑定发生在我初始签入 OnResume 之前?

是的,您可以在绑定上调用 executePendingBindings()

Evaluates the pending bindings, updating any Views that have expressions bound to modified variables. This must be run on the UI thread.