简体   繁体   中英

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

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.

在 AndroidManifest 文件中,在您的活动中添加android:configChanges="orientation|screenSize"

If you set id s 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"
       ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM