2

I'm new in this world of Android and I'm learning making an app, so I got a problem.

Working on

I created a fragment and inside I have a radio group containing 3 radio buttons

Goal

Clear all the radio buttons inside the fragment when the user returns to this screen

Problem

I don't know how to achieve that

Question

How to clear all checks of the radio buttons?

Steps done

I tried the following:

But it seems I can't do it

Code

This piece of code doesn't work for me (from the post above)

protected void onResume() 
{  
    RadioGroup rg=(RadioGroup)findViewById(R.id.RG);
    rg.clearCheck();  
    super.onResume();  
} 

But I have the following:

public class Operations extends Fragment
{
    RadioButton surfArea, rad, diam;
    RadioGroup radG;
    Button openSelect;

    public Operations()
    {
        // Required empty public constructor
    }

    public static Operations newInstance()
    {
        return new Operations();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);

        surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
        rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
        diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
        openSelect = (Button) rootView.findViewById(R.id.btn_open_select);

        //This piece is for testing purposes
        openSelect.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (surfArea.isChecked())
                {
                    Intent sa = new Intent(getContext(), OperSphere.class);
                    startActivity(sa);
                }
            }
        });

        return rootView;
    }
    //Here is where I'm stuck
    @Override
    public void onResume()
    {
        super.onResume();
    }
}

I'm interested in place the code inside onResume()

I know there is docs about fragments (https://developer.android.com/guide/components/fragments.html) but those can't answer my questions

Thanks in advance

Community
  • 1
  • 1
NAYIR55
  • 105
  • 5
  • 17

4 Answers4

1

1) Initialize your RadioGroup in onCreateView method as

private RadioGroup rg;

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);

    // initialize your radiogroup here
    rg = (RadioGroup) rootView.findViewById(R.id.RG);

    .....
    // Rest of your code
}

2) Now call the following method i.e.,clearSelection() wherever you want to UNCHECK the RadioButtons (but after above code).

private void clearSelection(){
    if(rg != null) rg.clearCheck();  
}

3) Example: If you want to uncheck the RadioButtons in onResume(), you can call it from there as

@Override
public void onResume(){
    super.onResume();

    clearSelection();
}

If you want to do it from some other method, it would work even then as

private void myMethod(){

    // need to clear radio buttons
    clearSelection();

    // rest of my code
}
0

have you tried placing your call super.onResume() before what you want to achieve...

RohitS
  • 1,036
  • 12
  • 17
  • yes, but Android Studio complains about that, it marks "Cannot resolve method `findViewById` " – NAYIR55 Sep 26 '16 at 16:12
  • can you explain what exactly you are doing here..where you can't find 'findViewById' ?? – RohitS Sep 26 '16 at 16:29
  • Android Studio is telling me that it cannot resolve this method `findViewById` inside `public void onResume()` and when I change `public` to `protected` it says " `onResume()` in (my package) clashes with `onResume` in `android.support.v4.app.Fragment` attempting to assign weaker access privileges – NAYIR55 Sep 26 '16 at 16:40
  • ok..first you cant assign weaker access 'from public to protected' dont do that...also try RadioGroup radG;= (RadioGroup) rootView.findViewById(R.id.RG); in you onCreateView and then in your Resume only use radG.clearCheck(); – RohitS Sep 28 '16 at 09:30
0

Call clearCheck() method of radioGroup in your onResume callback, something like:

@Override
protected void onResume() {  
super.onResume();  
rg.clearCheck();
    } 
0

For Anybody Who is facing the same problem While navigating between fragments can do this to make it work.

radioButton.setSaveEnabled(false);

Make sure you do that for all the radio buttons in the group and not the radioGroup itself

NOTE: This flag can only disable the saving of this view; any child views may still have their state saved.

Cyph3rCod3r
  • 1,978
  • 23
  • 33