Radio Button does not change text

Asked

Viewed 99 times

2

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

I tried to give request and invalidate and did not solve, it would 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: https://github.com/guilhermehrcosta/Radio-Group-Bug

  • Put the code directly into the question.

  • Ready! I put :D

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...
        }
    });

Handler should be started in its context, i.e., in the Activity that calls its fragment.

example:

Handler handler = new Handler();
  • 1

    Okay, now I ask you why setText() works, and when rotating the screen setText2() doesn’t work, it’s a crazy bug anyway. I decided to put a view.post(new Runnable());

  • 1

    That’s right William, the difference of using Handler.post(new Runnable) for the view.post(new Runable) is that Handler updates the whole context and the view only that view of the fragment in question. It is the same solution that I gave you, you could even put in the Textview type myTextView.post (new Runnable)) etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.