简体   繁体   中英

setChecked and setSelected in CheckBox not working

I have an Activity which has 5 fragments in pager. I used

 viewPager.setOffscreenPageLimit(fragmentList.size());

to be able to create all fragment in one time I also have a listener which will pass parameter(object) to all fragment once I have a newIntent.

in one of my fragment I have CheckBox which should be selected according to the parameter from activity. and other views which I were able to change the value of their texts and background. just this check box I have set it on But when I see it, I have see it is off, all other events works well.

here is the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme_PopupOverlay);
    setContentView(R.layout.activity_configurations);

    ViewPager viewPager = findViewById(R.id.container);
    TabLayout tabLayout = findViewById(R.id.tabs);

    setNdef(getIntent().getExtras());
    String type = getNdef().getString("id");

    List<Fragment> fragmentList = createFragments(type);
    SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragmentList);
    viewPager.setAdapter(pagerAdapter);

    fillTableLayout(tabLayout, type);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    viewPager.setOffscreenPageLimit(fragmentList.size());
    readNfcDialog = createReadDialog();
    writeNfcDialog = createWriteDialog();
    readNfcDialog.show();
    setForeground();
}

 @Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);

   
    ClassNfcTag wepTag = new ClassNfcTag(intent);
    String type = wepTag.defineClassTag();
    OmsRepeaterTag omsRepeaterTag = new OmsRepeaterTag(intent);
    byteMap = omsRepeaterTag.readTag();
   if (byteMap != null) {
   setTagOmsConfiguration(OmsConfiguration.fromByteArray(byteMap));
   for (OmsListener listener : onReceiveOmsList) {
        listener.gotOms(getInTagOmsConfiguration());
       }
   }
}

and here is one of the fragments

    @Override
public void gotOms(OmsConfiguration configuration) {
    setConfiguration(configuration);
    if (isAdded()) {
    boolean status = getConfiguration().getDeleteRsl();
    delete.setChecked(status);
    delete.setSelected(status);

    }
}

as a solution I have tried to use Switch instead of CheckBox and it worked well.

and I also tried to set the pager to start from exactly the fragment which has the CheckBox and it also work.

viewPager.setCurrentItem(checkBoxFragmentPosition);

I also tried to debug the code and It shows me that the checkBox is checked and when I tried to change it to checked by touching it (programmatically it is checked, in UI I see it unchecked) it change itself from unchecked to unchecked .and then with the next touch changed to checked.

just for the people who will read this question, simply this was an unresolved bug in android. For my case I used an toggleButton instead checkBox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM