Using android two way databinding with checkbox
Asked Answered
H

1

7

I'm trying to use android two-way databinding with checkBox in a fragment.
I have multiple fragments in viewPager with custom FragmentStatePagerAdapter and one viewModel in first fragment.
I have a checkbox with the code below:

<CheckBox
            android:id="@+id/checkbox_accept_rules"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="@={viewmodel.isAccept}"
            android:gravity="right"
            android:text="text..." />

In viewModel:

private final ObservableBoolean isAccept = new ObservableBoolean(false);

When I go to last fragment and return to first one the checkBox is not checked anymore, the color of the checkbox is true but the check icon is not there! Had anyone this problem before?

Thanks in advance

Holmberg answered 6/3, 2018 at 6:15 Comment(8)
I think it was a bug in android support library because after update problem was disappearHolmberg
@Sepher, which version to update?Bridgers
@Bridgers latest version 27.1.1Holmberg
@Sepher did you use AS 3.1.2 version as well? I've update the support library to 27.1.1, but still doesn't fix the checkbox problem T_TBridgers
@Bridgers yes , i use android studio 3.1.2 with gradle:3.0.0 if it helps!Holmberg
thank you very much yaaa, will check by tommorrow for the updates~Bridgers
@Sepehr Do you find any solution for this issue. Please guide me.Rag
I have the same issue, with the latest libraries.Pentode
C
0

View pager's fragment has an unique lifecycle than usual fragment.If you have three fragments A->B->C and you swipe B from C then fragment A will be destroyed.You can check it by override a method onDestroy in Fragment A.When you again swipe to A it will recreate again,and all the value will initialize again.That is the reason your checkbox's value is unchecked.Try to save checkbox's value in sharedPreference,so whenever it will recreate it will take the value's from sharedPreference and remeber to clear the sharedPrefence value when your viewpager will be destroyed or it'scontainer activity destroyed.

Carolyn answered 18/4, 2020 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.