I am attempting to save the state of checkboxes in a fragment. I have a test to make sure that the checkbox state is restored on an orientation change. The test fails because the checkbox state is not restored. However, when I step through the code during a test, it appears to work the way I expect. enabledFields has the correct values in onPause(), onSaveInstanceState() and onCreate(). The correct element of filterOptions is set to be checked. However, it doesn't appear as checked on the screen. What am I missing here?
FilterCardsTest
@RunWith(AndroidJUnit4.class)
public class FilterCardsTest {
@Rule
public ActivityTestRule<FragmentTestActivity> activityTestRule =
new ActivityTestRule<>(FragmentTestActivity.class);
private static final int[] IDS =
{R.id.brand, R.id.year, R.id.number, R.id.player_name, R.id.team};
private FragmentTestActivity activity = null;
private UiDevice device;
@Before
public void setUp() throws Exception {
activity = activityTestRule.getActivity();
activity.replaceFragment(new FilterCards());
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
@After
public void tearDown() throws RemoteException {
device.setOrientationNatural();
}
@Test
public void testSaveInstanceStateBrand() throws RemoteException {
this.testSaveInstanceState(R.id.brand);
}
private void testSaveInstanceState(int filterId) throws RemoteException {
this.testCheckBox(filterId);
device.setOrientationLeft();
onView(withId(filterId))
.check(matches(isChecked()));
onView(allOf(withParent(withId(filterId)), instanceOf(EditText.class)))
.check(matches(isEnabled()));
for (int id : IDS) {
if (id != filterId) {
onView(withId(id)).check(matches(isNotChecked()));
}
}
}
}
FilterCards
public class FilterCards extends Fragment {
private static final String INPUT_EXTRA = "input";
private static final String[] EXTRAS = {BRAND_EXTRA, YEAR_EXTRA, NUMBER_EXTRA,
PLAYER_NAME_EXTRA, TEAM_EXTRA};
@InjectViews({R.id.brand, R.id.year, R.id.number, R.id.player_name, R.id.team})
List<FilterOptionView> filterOptions;
private ArrayList<Integer> enabledFields = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.filter_cards, container, false);
ButterKnife.inject(this, view);
// restore input fields state
if (savedInstanceState != null) {
ArrayList<Integer> enabledFields = savedInstanceState
.getIntegerArrayList(INPUT_EXTRA);
Log.d(TAG, "enabledField=" + enabledFields);
if (enabledFields != null) {
for (int i : enabledFields) {
filterOptions.get(i).setChecked(true);
}
}
}
return view;
}
@Override
public void onPause() {
super.onPause();
enabledFields.clear();
for (int i = 0; i < filterOptions.size(); i++) {
if (filterOptions.get(i).isChecked()) {
enabledFields.add(i);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntegerArrayList(INPUT_EXTRA, enabledFields);
}
}
Aucun commentaire:
Enregistrer un commentaire