4

I have options menu in my toolbar with radibutton item :

 <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 selected radiobutton when orientation change happened in onSaveInstanceState,onRestoreInstanceState but i can't understand how to get selected button from radiogroup in options menu.

Lester
  • 2,544
  • 4
  • 27
  • 38

3 Answers3

6

Here is a fully working and tested example. With this code in place, no matter how many times you rotate the screen, the currently selected item will persist.

First, create these instance variables to keep track of the state of the menu and have a name for the preference you will be saving in the Bundle:

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

The saveInstanceState() method should save off the currently selected value:

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

Update the currently selected item in 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);
}

In onCreate(), load the saved data if it exists:

@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;
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
1

MenuItem in onOptionsItemSelected has item.isChecked() method, just use it. You can store one boolean field(it wouldn't be a bad thing in my opinion) and change it whenever change in radiog group occurs

Then you can have your id by simply calling:

if(item.isChecked()) {
     your_id_field = item.getItemId()
}
Nik Myers
  • 1,843
  • 19
  • 28
  • So it will be three boolean variables for each radibutton. It would be better if i could save selected id or something – Lester May 11 '15 at 20:42
  • It's your descision, i just wrote a solution(i thought it was), i didn't know that how you will store it is part of a question, sry, you can store itemId, yes – Nik Myers May 11 '15 at 20:44
  • Thanks for answer,basically my question was about getting selected id from radiogroup in optionsmenu.I dont know way to do it – Lester May 11 '15 at 20:46
  • i've made changes to my answer – Nik Myers May 11 '15 at 20:48
0

Create a variable eg: menu_selection to store the id of the menu item selected. Initially you can define 0

private int menu_selection = 0;

Save and restore the value of the variable using onSaveInstanceState and onRestoreInstanceState

@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 id of the menu item selected item.getItemId() to the 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;
}