PopupMenu with checkableBehavior="single" group ignores setCheck() on items - android

I have a PopupMenu define in XML below being part of a Fragment
<menu xmlns:com.itrash.android="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/order_by_menu" android:checkableBehavior="single">
<item android:id="#+id/action_show_discounts_only"
android:title="#string/discounts_only_title" />
<item android:id="#+id/action_show_all"
android:title="#string/all_title" />
</group>
The menu is linked to ImageButton (snippet from onActionCreated() )
final SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
this.onlyDiscounts = prefs.getBoolean(PROPERTY_ONLY_DISCOUNTS, false);
this.orderBy = prefs.getString(PROPERTY_ORDER_BY, ORDER_BY_NAME);
this.filterButton = (ImageButton) getActivity().findViewById(R.id.filter_button);
this.filterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
filterMenu.show();
}
});
this.filterMenu = new PopupMenu(getActivity(), getActivity().findViewById(R.id.filter_button));
this.filterMenu.inflate(R.menu.product_filter_menu);
this.filterMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
onFilterClick(item);
return true;
}
});
When clicked the following method is called
private void onFilterClick(MenuItem item) {
boolean showDiscountsOnly = item.getItemId() == R.id.action_show_discounts_only ? true : false;
item.setChecked(!item.isChecked());
if (this.onlyDiscounts != showDiscountsOnly) {
this.onlyDiscounts = showDiscountsOnly;
productSearchResultAdapter.clear();
this.curPage = 1;
doSearchProducts();
}
final SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PROPERTY_ONLY_DISCOUNTS, showDiscountsOnly);
editor.commit();
}
Problem I am facing is how to restore state of menu items according to attribute onlyDiscounts when retrieved from SharedPreferences?
In other words I want to have the menu item *action_show_discounts_only* checked if onlyDiscounts is true.
I have tried to place inside onActionCreated() something along the lines
for (int i = 0; i < this.filterMenu.getMenu().size(); i++) {
MenuItem item = this.filterMenu.getMenu().getItem(i);
if (item.getItemId() == R.id.action_show_discounts_only) {
item.setChecked(true);
} else {
item.setChecked(false);
}
}
or even
this.filterMenu.getMenu().findItem(R.id.action_show_discounts_only).setChecked(this.onlyDiscounts);
this.filterMenu.getMenu().findItem(R.id.action_show_all).setChecked(!this.onlyDiscounts);
Both attempts were ignored, *R.id.action_show_all* item is always check no matter what value onlyDiscounts is.
Any idea how to resolve this?

Related

Make menu item on android work like a button

I'm here to see if they guide me in this problem
I have a menu, with an item, and what I want is that item to work as a button, if you click on it change the icon and a toast that says Notification active, and if you click again, change the icon and say using toast, Disabled notifications.
All this I have achieved, the detail is that when activating the button and leaving the activity and when entering again, the button is again deactivated, when it should be activated
I hope you understand, here I give you the code, which makes the function perfect, but I think I must do something with shared preferences to save if it is clicked or not, but I do not know where to start, I hope you understand and guide me, I leave the code that I have implemented
----- xml menu ------------
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="#drawable/ic_notifications_off_black_24dp"
android:title="#string/notificaciones"
android:id="#+id/notificaciones"
android:checkable="true"
app:showAsAction="always"
/>
</menu>
--------- Java---------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menucampeonatos, menu);
MenuItem item = menu.findItem(R.id.notificaciones);
item.setChecked(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.notificaciones);
checkable.setChecked(false);
return super.onPrepareOptionsMenu(menu);
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.notificaciones:
if (item.isChecked()){
myRef.update("notificacion", false);
item.setChecked(false);
item.setIcon(R.drawable.ic_notifications_off_black_24dp);
Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();
}else{
myRef.update("notificacion", true);
item.setChecked(true);
item.setIcon(R.drawable.ic_notifications_active_black_24dp);
Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
First you need to implement Shared Preferences, as you mentioned
private String sharedPrefFile = "your Shared Preferences Name";
private String NOTIFICATION_KEY = "notification_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
...
}
Then, in your onOptionsItemSelected, you need to save user action to preference
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.notificaciones:
if (item.isChecked()){
// Add this line
SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putBoolean(NOTIFICATION_KEY, true);
preferencesEditor.apply();
myRef.update("notificacion", false);
item.setChecked(false);
item.setIcon(R.drawable.ic_notifications_off_black_24dp);
Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();
}else{
// Add this line
SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putBoolean(NOTIFICATION_KEY, false);
preferencesEditor.apply();
myRef.update("notificacion", true);
item.setChecked(true);
item.setIcon(R.drawable.ic_notifications_active_black_24dp);
Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
Lastly, load the preference everytime activity is destroyed in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.notificaciones);
String isNotificationClicked = mPreferences.getBoolean(NOTIFICATION_KEY, false);
checkable.setChecked(isNotificationClicked);
return super.onPrepareOptionsMenu(menu);
}

Save optionMenu item icon to shared preferences

I have a list of data with two different views in my Fragment, my default view is Listview and another is Gridview. I switch between this two views by clicking on an icon on my toolbar.
And I set the switch item icon dynamically inside onOptionsItemSelected method like this:
//global variables
private int currentViewMode = 0 ;
static final int VIEW_MODE_LISTVIEW = 0;
static final int VIEW_MODE_GRIDVIEW = 1;
.
.
.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.switchView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("currentViewMode",currentViewMode);
editor.commit();
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
And I get my view from shared preferences like this:
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",activity.MODE_PRIVATE);
currentViewMode = sharedPreferences.getInt("currentViewMode",VIEW_MODE_LISTVIEW);
But I don't know how can I save the item icon and how to retrive it.
Can you help me please?
Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/switchView"
android:title=""
android:icon="#mipmap/ic_listview"
app:showAsAction="always"/>
</menu>
You can add another item inside your menu.xml for your gridView Then create a boolean that carries the visibility of this two items. when you switch to listview, set the visibility of gridItem to false, And when you switch to gridview, set the listItem to false; In this way you'll be able to save and retrieve the visibilities with shared preferences as a boolean.
Change your code like this:
<item
android:id="#+id/switchListView"
android:title=""
android:icon="#mipmap/ic_listview"
app:showAsAction="always"/>
<item
android:id="#+id/switchGridView"
android:title=""
android:icon="#mipmap/ic_gridview"
android:visible="false"
app:showAsAction="always"/>
Inside your Fragment:
boolean isGridView;
Then:
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
isGridView = myPrefs.getBoolean("menu_item", false);
And finally change your onOptionItemSelected method like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
final SharedPreferences.Editor editor = myPrefs.edit();
isGridView = myPrefs.getBoolean("menu_item", false);
switch (item.getItemId()) {
case R.id.switchListView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
isGridView=true;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
isGridView=false;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
SharedPreferences sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
SharedPreferences.Editor et = sharedPreferences.edit();
et.putInt("currentViewMode",currentViewMode);
et.commit();
return false;
case R.id.switchGridView:
if (VIEW_MODE_LISTVIEW == currentViewMode){
isGridView=true;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_gridview);
currentViewMode = VIEW_MODE_GRIDVIEW;
}else {
isGridView=false;
editor.putBoolean("menu_item", isGridView);
editor.commit();
activity.invalidateOptionsMenu();
item.setIcon(R.mipmap.ic_listview);
currentViewMode = VIEW_MODE_LISTVIEW;
}
switchView();
sharedPreferences = activity.getSharedPreferences("ViewMode",currentViewMode);
et = sharedPreferences.edit();
et.putInt("currentViewMode",currentViewMode);
et.commit();
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
if(isGridView==true){
menu.findItem(R.id.switchListView).setVisible(false);
menu.findItem(R.id.switchGridView).setVisible(true);
}else{
menu.findItem(R.id.switchListView).setVisible(true);
menu.findItem(R.id.switchGridView).setVisible(false);
}
super.onPrepareOptionsMenu(menu);
}
Important note
How to implement optionMenu inside Fragments:
1. If you have implemented optionMenu inside your MainActivity, then you should return false in your MainActivity's onOptionsItemSelected, otherwise your Fragment's optionMenu will not work.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
//
}
return false;
}
2. you have to setHasOptionsMenu(true); inside your onCreate in your Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

Android BottomNavigationView menuItem set checked doesn't work properly

In my bottomNavigationView I can change programmaticaly my menu items everything it seems to work, but in fact my layout is diferent from if is checked or if I click directly on tab. Please look at differences bellow:
1) for menuItem.setChecked(true):
2) when a user clicks directly on icon it will perform the animation and show it on the correct
Well what I real want is to select the menuItem as if the user clicked. Isn't supposed to work by doing menuItem.setChecked(true)?
Please take into consideration the following definition layout for menu items:
<item android:id="#+id/menu_home"
android:title="#string/menu_home"
android:icon="#drawable/ic_home_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_hall_of_fame"
android:title="#string/menu_hall_of_fame"
android:icon="#drawable/ic_stars_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_info"
android:title="#string/menu_info"
android:icon="#drawable/ic_info_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_black_24dp"
app:showAsAction="ifRoom" />
and how I'm checking the correct menuItem:
MenuItem item = mBottomNav.getMenu().findItem(R.id.menu_about);
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items and select the one.
for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == mSelectedItem);
}
There is another method to perform a selection of a menuItem? What am I missing here?
See This i post here all code
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
frag = MenuFragment.newInstance(getString(R.string.text_home),
getColorFromRes(R.color.color_home));
break;
case R.id.menu_notifications:
frag = MenuFragment.newInstance(getString(R.string.text_notifications),
getColorFromRes(R.color.color_notifications));
break;
case R.id.menu_search:
frag = MenuFragment.newInstance(getString(R.string.text_search),
getColorFromRes(R.color.color_search));
break;
}
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items.
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}
updateToolbarText(item.getTitle());
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
private int getColorFromRes(#ColorRes int resId) {
return ContextCompat.getColor(this, resId);
}
}
you can use this code
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}

How to get MenuItem position in the listener using the new NavigationView

The topic says it all. How should I go about retrieving the item position on the onClick listener using NavigationView? Also, why is there no getHeader method? Lastly I am doing everything programmatically, but the header is still clickable. Any thoughts?
Thanks!
I found a simple solution. You can assign an order using Menu's add(...) method. Then you can retrieve the order using MenuItems's getOrder(...) method. If you are using xml, you can use android:orderInCategory="...".
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
Menu menu = navigationView.getMenu();
for(int i=0; i < menu.size(); i++){
items.add(Menu.NONE, Menu.NONE, i, menu.getItem(i));
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
int position=items.getOrder();
return true;
}
});
UPDATE
You can get position using this trick
final List<MenuItem> items = new ArrayList<>();
Menu menu;
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);.
menu = navigationView.getMenu();
for(int i = 0; i < menu.size(); i++){
items.add(menu.getItem(i));
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
int position = items.indexOf(menuItem);
return true;
}
});
You can just take its order if you specify the "android:orderInCategory" attribute for menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:orderInCategory="0"
android:title="#string/news" />
<item
android:orderInCategory="1"
android:title="#string/search" />
</menu>
val navigationView = findViewById<NavigationView>(R.id.navigation)
navigationView.setNavigationItemSelectedListener { menuItem ->
val menuItemOrder = menuItem.order
true
}
Or, use this in case you don't want to specify orders by hand:
val navigationView = findViewById<NavigationView>(R.id.navigation)
navigationView.setNavigationItemSelectedListener { menuItem ->
val menuItemIndex = bottomNavigation.menu.children.indexOf(menuItem)
true
}
If you are using menu_drawer.xml, you just have to add an id in the items like this:
<item
android:id="#+id/nav_top_stories"
android:title="#string/txt.menu.item1"
/>
With this you just have to test on menuItm.getId():
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
switch(menuItem.getId()){
case R.id.txt_menu_item1 : //do what you want to do;
break;
case R.id.txt_menu_item2 : // etc,
}
return true;
}
});
If you are using dynamic menu, just use this method to add an item to you navigation drawer:
NavigationView.getMenu().add(int groupId, int itemId, int order, CharSequence title)
And then test by the order:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
// update highlighted item in the navigation menu
menuItem.setChecked(true);
switch(menuItem.getOrder()){
case 0 : //do what you want to do;
break;
case 1 : // etc,
default : //do whatever you want ;
}
return true;
}
});
In my case, i use
first test whit this..
Log.d(TAG, navigationView.getMenu().getItem(0).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(1).isChecked());
Log.d(TAG, navigationView.getMenu().getItem(2).isChecked());
next this...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(navigationView.getMenu().getItem(0).isChecked()) {
cerrarSesion();
}else {
navigationView.getMenu().getItem(0).setChecked(true);
seleccionarItem(navigationView.getMenu().getItem(0));
}
}
return false;
}
The easy, and most clean (in my opinion) way:
private int getBottomNavigationItemPosition(#MenuRes int itemId) {
final Menu bottomNavigationMenu = mBottomNavigationView.getMenu();
final int menuItemCount = bottomNavigationMenu.size();
for (int i = 0; i < menuItemCount; i++) {
if (bottomNavigationMenu.getItem(i).getItemId() == itemId) return i;
}
return -1; // the item with itemId was not found in Menu
}

How to save instance state of selected radiobutton on menu

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.
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;
}
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()
}
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;
}

Categories

Resources