Vid
Vid

Reputation: 1012

How to save radio button checked on orientation change?

I have an Activity with a FrameLayout to load fragment and a RadioGroup with four radio button. Let's say RadioButton rb1, rb2, rb3, rb4 will load different Fragment F1, F2, F3, F4 respectively on the checkChanged and default it will load F1 fragment.

Now, if i am selecting rb2 then it is loading fragment F2 and after orientation change again it is loading F1. Bellow is my code, please give any suggestion how to handle it.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioWidget = (RadioWidget) findViewById(R.id.segmented_control);
        mRadioWidget.mRGroup.setOnCheckedChangeListener(this);

        if (savedInstanceState == null) {
            HomeFragment homeFragment = new HomeFragment();
            loadFragment(false, homeFragment, HomeFragment.CLASSTAG);
        }
    }

@Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {

        case R.id.rb_home:

            HomeFragment homeFragment = new HomeFragment();
            loadFragment(false, homeFragment, HomeFragment.CLASSTAG);
            break;

        case R.id.rb_config:

            ConfigFragment configFragment = new ConfigFragment();
            loadFragment(false, configFragment, ConfigFragment.CLASSTAG);
            break;

        case R.id.rb_flash:

            Toast.makeText(getApplicationContext(), "Not yet implemented", Toast.LENGTH_LONG).show();
            break;

        case R.id.rb_fav:

            FavoritesFragment favFragment = new FavoritesFragment();
            loadFragment(false, favFragment, FavoritesFragment.CLASSTAG);
            break;
        }
    }

Upvotes: 0

Views: 1504

Answers (3)

DanielJaramillo
DanielJaramillo

Reputation: 334

If you set ids attributes to <RadioButton> views in you .mxl file, the system will save the state for you, same as happens with <EditText> views. It doesn't work if you only set the id for <RadioGroup>.

For example

Before (bad)

    <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ... />

Now

    <RadioButton
       android:id="@+id/radio_example" # <----- missing property
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ... />

Upvotes: 0

Shivam Verma
Shivam Verma

Reputation: 8023

You should try to remember the fragment that was last loaded by putting some sort of a value in the savedInstanceState Bundle.

This is how you can save and retrieve data : Saving Android Activity state using Save Instance State

When in onCreate the bundle is not null, retrieve that value from the bundle, and then load the correct fragment and update the checkbox.

Upvotes: 2

Arun Badole
Arun Badole

Reputation: 11097

In AndroidManifest file add android:configChanges="orientation|screenSize" in your activity.

Upvotes: 2

Related Questions