1

I have some RadioButton in fragmentA,,,and also some RadioButton in fragmentB i want that when one of these isChecked(FragmentA),in FragmentB disable all radio button. same activity host these fragment.

event click Radio Buttons in Fragment A:

   RadioGroup radioGroup = view.findViewById(R.id.readingGroup);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            switch (i) {
                case R.id.btnNormalD:
                    //code
                    break;
                case R.id.btnAbsence:
                    //code
                    break;
                case R.id.btnWithAnObstacle:
                    //code
                    break;
                case R.id.btnHaunted:
                    //code
                    break;
                case R.id.btnDestroyed:
                    //code
                    break;
                case R.id.btnNoSubscribersFound:
                    //code
                    break;
                case R.id.btnGround:
                    //code
                    break;
                case R.id.btnInterrupted:
                    //code
                    break;

            }
        }
    });
Amir251
  • 29
  • 8
  • Possible duplicate of [Basic Communication between two fragments](https://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments) – Niko Dec 24 '17 at 07:41

1 Answers1

0

If your fragments share same container except ViewPager then You can pass data between fragments using Bundle. Whenever you making transaction set the state of radio buttons to bundle and validate it into next fragment .

 Fragment fragment=new FragmentA();
    Bundle bundle=new Bundle();
    bundle.putBoolean("show",false);// Pass the data here
    fragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction()
            .add(fragment,R.id.frame).commit();

And you can get this in next fragment .

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_2,null);
    if(getArguments()!=null){
        boolean show=getArguments().getBoolean("show",true);
        if(show){
            // Do action here 
        }else{
            // Do action here
        }
    }
    return view;
}

If you are using `ViewPager' then you can simply get the fragment from adapter and call any public method of this fragment to set the state .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • my fragments don't have id,because these Initialize in view pager adapter – Amir251 Dec 24 '17 at 07:54
  • What do you mean don't have id? Are you developing for tablet? – ADM Dec 24 '17 at 07:55
  • these base on `int position` in view pager are in position(`public Fragment getItem(int position)`). – Amir251 Dec 24 '17 at 08:00
  • Ohkk . have a look into [this thread](https://stackoverflow.com/questions/20474695/communication-between-fragments-in-viewpager). – ADM Dec 24 '17 at 08:02
  • @ADM hey, if I want to click fragment's radio button then FragmentB's LinearLayout will be visible. is it possible to make? how can I implement it? guide me, here is the where i posted it, please.https://stackoverflow.com/questions/68063328/checked-radiobutton-in-onefragment-to-anotherfragment – Ibnul Imtiaz Aug 17 '21 at 12:07