Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting RadioButton.setChecked with Multiple Fragments

Hey I have a really stupid problem and I can't figure out why it is not working as expected. So I have a MultiFragment layout (each one has some different questions) using viewpager and FragmentStatePagerAdapter. When I open the screen that hosts all these fragments I am trying to restore the previous state (marking all answered questions) using a network call. However it seems that if my Fragment is not visible to the user it can't update the checked state of the radio button / checkboxes.

enter image description here

Does anybody know what I can do to achieve the wished behavior?

Cheers and thanks in advance!

 @Override
 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
     super.onViewCreated(view, savedInstanceState);
     presenter.restoreAnswersFromPreviousSession(questionId);
}

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisible()) {
        if (isVisibleToUser) {
            presenter.restoreAnswersFromPreviousSession(questionId);
            Log.d("Fit", "My Fragment is visible");
        } else {
                   Log.d("Fit", "My Fragment is not visible");
               }
    }
}

Here I restore the state (called after the request is successful)

previousReplies = repliesToRestore;
for (QualityReportReply reportReply : repliesToRestore) {
  int id = reportReply.id();
  switch (id) {
    case 201: {
      boolean tooThin = (boolean) reportReply.value();
      if (tooThin) {
        materialTooThinGroupYes.setChecked(true);
      } else {
        materialTooThinGroupNo.setChecked(true);
      }
      break;
    }
    case 202: {
      boolean tooThick = (boolean) reportReply.value();
      if (tooThick) {
        materialTooThickGroupYes.setChecked(true);
      } else {
        materialTooThickGroupNo.setChecked(true);
      }
      break;
    }
    case 203: {
      boolean drawingThreads = (boolean) reportReply.value();
      if (drawingThreads) {
        materialDrawThreadsGroupYes.setChecked(true);
      } else {
        materialDrawThreadsGroupNo.setChecked(true);
      }
      break;
    }
    case 204: {
      boolean flyingThreads = (boolean) reportReply.value();
      if (flyingThreads) {
        materialFlyingThreadsGroupYes.setChecked(true);
      } else {
        materialFlyingThreadsGroupNo.setChecked(true);
      }
      break;
    }
    case 205: {
      boolean knots = (boolean) reportReply.value();
      if (knots) {
        materialKnotsGroupYes.setChecked(true);
      } else {
        materialKnotsGroupNo.setChecked(true);
      }
      break;
    }
like image 447
dhartwich Avatar asked Apr 06 '17 07:04

dhartwich


People also ask

How many Radiobuttons in a frame can be selected at the same time?

Therefore only one RadioButton at a time can be selected, even if it is a part of a functional group. You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form.

Is it necessary to use RadioGroup when you want to use RadioButton to choose one item at a time?

RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.

How do I change the default value of a radio group?

RadioGroup radioGroup = new RadioGroup(context); RadioButton radioBtn1 = new RadioButton(context); RadioButton radioBtn2 = new RadioButton(context); RadioButton radioBtn3 = new RadioButton(context); radioBtn1. setText("Less"); radioBtn2. setText("Normal"); radioBtn3. setText("More"); radioBtn2.

How do I get radio buttons side by side on android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.


1 Answers

UPDATE

Here is a working example, you can see it.

I think you are restoring state on the host Activity. Don't do this, do it in each Fragment. Let's say you have two fragments, Passform and Material, so instead of doing it in Activity, make a call inside fragment's onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_passform, container, false);
    new FetchPassFormState().execute();
    return view;
}

Update views inside onPostExecute of FetchPassFormState or wherever you want, as per your need.

UPDATE

You can do one thing. You must be adding Fragments in your QuestionsViewPagerAdapter, try to setArguments of your Fragments there. Like,

Fragment fragment = new YourFragment();
Bundle bundle = new Bundle();
bundle.putString("your_key", "Your Data");
...
fragment.setArguments(bundle);

Now add this fragment. And inside Fragment's onCreateView, get this Bundle.

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_material_questions, container, false);
    ButterKnife.bind(this, view);
    getFragmentComponent().inject(this);
    presenter.setView(this);

    Bundle bundle = getArguments();
    String youdData = bundle.getString("your_key");
    ...
}

And then use this data to set views.

like image 114
Waqas Ahmed Ansari Avatar answered Sep 24 '22 00:09

Waqas Ahmed Ansari