Android : calling onOptionsItemSelected programatically don't working as expected

on Sunday, September 14, 2014


I'm developing an app for smartphones and tables using fragments. When the app is executed in smartphones, there are two activities, the first handles a categories ListView and the second handles a detail ListView and each activity has its own ActionBar items. When the app is executed in tablets, I must load a new ActionBar with the items of both activities of the smartphone execution.


The problem is that in the tablet menu, there are two MenuItems that can do two different actions each one. For example, sharing is available on the first activity to share the app and is also available on the second activity to share other things.


To do this, I've added a conditional: when it's a tablet, show an AlertDialog.Builder with the two options available of sharing. This is working great, the problem is that the other MenuItem that has two different actions is not working as expected. This is the situation:


The MenuItem should show/hide some favourites of the app. The structure is the following:


Activity containing two fragments -> First fragment is the categories ListView (first activity in smartphone); Second fragment is the detail ListView (second activity in smartphone). Something like this:


Example of my app


The right fragment (the detail one) contains a ViewPager that inflates another ListView that can contain normal detail, or favourites. By default it shows normal details, but user can load its favourite detail. The problem here is that this MenuItem also changes the categories to favourite categories, so when clicking this MenuItem an AlertDialog.Builder appears asking to the user what favourites want him to load (categories or detail). The pressed button is the icShowFavourites. To do this I am using this code:



@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
switch (id) {
case R.id.icShowFavourites:
if (isTablet) {
// The problem resides here!!! <----------------------
if (showFavouriteArticles) {
showFavouriteArticles = false;
return false;
} else {
showFavouritesDialog(item);
return true;
}
} else {
frPortalsList.showHideFavourites();
return true;
}
break;
default:
break;
}


private void showFavouritesDialog(final MenuItem item) {
final CharSequence options[] = new CharSequence[] {"OPT1", "OPT2"};
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose OPT");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
frPortalsList.showHideFavourites();
frPagerFragment.showHideFavourites();
break;
case 1:
showFavouriteArticles = true;
onOptionsItemSelected(item);
break;
default:
break;
}
}
});
builder.show();
}


Code explanation: When the user clicks the icShowFavourite MenuItem for the first time in tablet mode, the boolean showFavouriteArticles is false, so the AlertDialog.Builder appears and asks the user what to do. This is working well. The problem is when the user clicks on the case 1 option of the AlertDialog.Builder. The boolean showFavouriteArticles changes to true and then I invoke programmatically onOptionsItemSelected(item) passing the MenuItem that the onOptionsItemSelected catched so that it executes the return false to let the fragments do their actions.


The weird thing here is that this code WORKS:



@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
switch (id) {
case R.id.icShowFavourites:
if (isTablet) {
// Now it works!!! <----------------------
return false;

} else {
frPortalsList.showHideFavourites();
return true;
}
break;
default:
break;
}


So it seems that when I call onOptionsItemSelected(item) programatically, it do not let the fragments execute the onOptionsItemSelected(item).


What can I do?


Thank you in advance!


0 comments:

Post a Comment