The Radio Button does not change the text

2

I created a simple project, and realized that RadioButton does not change the text when rotating the screen. I created 2 functions one called setText() and the other setText2() with different contents, setText() is called when savedInstance is null and the other when null is null, android enters setText2() normally and arrow text , but does not change visually.

I tried to give request and invalidate and it did not solve, would it be a component bug?

public class SampleFragment extends Fragment {
    private RadioButton optA, optB,optC,optD;
    private static final String TAG = "SampleFragment";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView");
        View view  = inflater.inflate(R.layout.fragment_sample,container,false);
        optA =  (RadioButton) view.findViewById(R.id.optA);
        optB =  (RadioButton) view.findViewById(R.id.optB);
        optC =  (RadioButton) view.findViewById(R.id.optC);
        optD =  (RadioButton) view.findViewById(R.id.optD);
        Button btnSetText = (Button) view.findViewById(R.id.btn_settext);
        btnSetText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setText2();
            }
        });
        if(savedInstanceState == null){
            setText();
        }
        else{
            setText2();
        }

        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG,"onPause");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG,"onResume");
    }
    private void setText2(){
        optA.setText("A1");
        optB.setText("B2");
        optC.setText("C3");
        optD.setText("D4");
    }
    private void setText(){
        optA.setText("A");
        optB.setText("B");
        optC.setText("C");
        optD.setText("D");
    }

}

Github of the project: link

    
asked by anonymous 20.12.2015 / 23:03

1 answer

3

You need to send a message to your user interface stating that you want to update a component.

You can use a Handler as follows:

handler.post(new Runnable() {
        @Override
        public void run() {
            // SETA O TEXTO NO SEU COMPONENTE AQUI...
        }
    });

The Handler should be started in its context, that is, in the Activity that calls its fragment.

example:

Handler handler = new Handler();
    
21.12.2015 / 20:30