How to save the instance state of the selected radio exchange in the menu

I have an options menu in my toolbar with a radibutton element:

 <item
    android:id="@+id/map_menu"
    android:icon="@drawable/ic_layer"
    android:orderInCategory="102"
    app:showAsAction="always"
    android:title="@string/action_settings">
    <menu>
        <group
            android:id="@+id/map_types_group"
            android:checkableBehavior="single" >
            <item
                android:id="@+id/map_terrain"
                android:orderInCategory="1"
                app:showAsAction="ifRoom"
                android:title="@string/map_terrain"/>
            <item
                android:id="@+id/map_normal"
                android:orderInCategory="2"
                android:checked="true"
                app:showAsAction="ifRoom"
                android:title="@string/map_normal"/>
            <item
                android:id="@+id/map_hybrid"
                android:orderInCategory="3"
                app:showAsAction="ifRoom"
                android:title="@string/map_hybrid"/>
        </group>
    </menu>
</item>

      

I want to restore the selected radio when the orientation change happened in onSaveInstanceState, onRestoreInstanceState, but I can't figure out how to get the selected button from the radiogroup in the options menu.

+3


source to share


3 answers


Here is a complete working and tested example. With this code, no matter how many times you rotate the screen, the currently selected item will be kept.

First, create these instance variables to keep track of the state of the menu and have a name for the preference that you will save in the package:

private final static String MENU_SELECTED = "selected";
private int selected = -1;
MenuItem menuItem;

      

The method saveInstanceState()

should save the currently selected value:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt(MENU_SELECTED, selected);
    super.onSaveInstanceState(savedInstanceState);
}

      

Update the currently selected item to onOptionsItemSelected()

:



@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        Log.d("settings", "id: " + id);
        return true;
    }

    if (id == R.id.map_terrain){
        Log.d("menuitem", "terrain id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    if (id == R.id.map_normal){
        Log.d("menuitem", "normal id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    if (id == R.id.map_hybrid){
        Log.d("menuitem", "hybrid id: " + id);
        selected = id;
        item.setChecked(true);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

      

The onCreate()

download stored data, if they exist:

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

    if (savedInstanceState != null){
        selected = savedInstanceState.getInt(MENU_SELECTED);
    }
}

      

And then re-select the previously selected item in onCreateOptionsMenu()

:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    if (selected == -1){
        return true;
    }

    switch (selected){
        case R.id.map_terrain:
            menuItem = (MenuItem) menu.findItem(R.id.map_terrain);
            menuItem.setChecked(true);
            break;

        case R.id.map_normal:
            menuItem = (MenuItem) menu.findItem(R.id.map_normal);
            menuItem.setChecked(true);
            break;

        case R.id.map_hybrid:
            menuItem = (MenuItem) menu.findItem(R.id.map_hybrid);
            menuItem.setChecked(true);
            break;
    }

    return true;
}

      

+5


source


MenuItem

in onOptionsItemSelected

has a method item.isChecked()

, just use it. You can keep one boolean field (it won't be that bad in my opinion) and change it whenever there is a change in the radio group.

Then you can get your id by simply calling:



if(item.isChecked()) {
     your_id_field = item.getItemId()
}

      

+1


source


Create a variable like: menu_selection

to store the ID of the selected menu item. Initially you can define 0

private int menu_selection = 0;

      

Save and restore the value of the variable with onSaveInstanceState

andonRestoreInstanceState

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Save the id of radio button selected in the menu
        savedInstanceState.putInt("selection", menu_selection);

        super.onSaveInstanceState(savedInstanceState);
    }




@Override
    public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);

        // Restore state members from saved instance
        menu_selection = savedInstanceState.getInt("selection");

    }

      

In onOptionsItemSelected

assign the identifier of the selected menu item to a item.getItemId()

variable

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection

    if (item.isChecked()) {
        item.setChecked(false);
    } else {
        item.setChecked(true);
        menu_selection = item.getItemId();
    }

    switch (item.getItemId()) {

        case R.id.map_terrain:
            //some action here
            return true;

        case R.id.map_normal:
            //some action here  
            return true;

        case R.id.map_hybrid:
            //some action here
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

      

In onCreateOptionsMenu

find the menu item identified with the value in the variable and check it

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

    if (menu_selection != 0) {

        MenuItem selected = (MenuItem) menu.findItem(menu_selection);
        selected.setChecked(true);
     }

    return true;
}

      

0


source







All Articles