0

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;
        }
    }
Vid
  • 1,012
  • 1
  • 14
  • 29

3 Answers3

2

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

Arun Badole
  • 10,977
  • 19
  • 67
  • 96
  • 2
    Adding this to the activity tag will prevent the issue from happening but this is not the way it should be done because this prevents the activity from recreating which leads to other issues such as the correct layout not being set for the landscape mode and all such cases will need to be handled by the developer explicitly in the code. – Shivam Verma Jan 23 '15 at 11:32
2

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.

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

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"
       ... />
DanielJaramillo
  • 181
  • 1
  • 4
  • 13