Closing Options Menu on orientation change in Android - android

this is my first post here so hello everyone and forgive me any mistakes common for newcomers.
In my actual Android 4.0 project im using Options Menu which is opened by clicking on one of ActionBar items. Items of my menu are asynchronously updated so in some cases it opens before changes has been made and this is proper behaviour for me. As we know, after user changes an orientation of the device, whole activity is recreated (same for its menu). This use case is properly handled in my code - state of Activity is saved.
Problem happens when user opens Options Menu and changes orientation when menu is still visible - menu gets recreated and shown. I would like to make it not appear after it has been created.
Is it even possible? I assume that i should do something either in onCreateOptionsMenu() or in onPrepareOptionsMenu() method.

You could use closeOptionsMenu() to close your OptionsMenu manually. If you want to open it at any later time, you could call openOptionsMenu().

I can think of a workaround. set a class variable true in onRestoreInstanceState. override onPrepareOptionsMenu return false if that variable is true and set that variable to false again. It should give you the behavior you want. comment if there's any problem, I'll post the code.

Related

How do I only trigger onPrepareOptionsMenu and not onCreateOptionsMenu?

According to the Android menu documentation and a similar question, onPrepareOptionsMenu should be used to update menus dynamically. For Android 3.0 and higher, the documentation says:
When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu()
However, when calling invalidateOptionsMenu(), the system calls both, onCreateOptionsMenu() and onPrepareOptionsMenu(). Resuming the activity also either calls both methods (if it was stopped in background) or none.
To me, this sounds like the differentiation between the two methods is obsolete. Is there any situation in which Android only calls onPrepareOptionsMenu() but not onCreateOptionsMenu()? Additionally, is there a way to manually request that Android only calls onPrepareOptionsMenu(), like it is implied in the documentation (cited below)?
[onCreateOptionsMenu()] is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
Thank you very much!
You are using Activity menu here, so assuming you have single activity app it will be only inflated once, during onCreateOptionsMenu - that's the scenario with a single call.
If you want to change it, then you have to invalidate the current one and inflate a new one - as it's an app's global menu it only make sense if you want to change its contents - you do that using onPrepareOptionsMenu and then inflate it again in onCreateOptionsMenu call.
But if you make lots of menu changes then it amy be better to use to use Fragment specific menus - they only live as long as the their fragment - menus
So to answer your specific question - you cannot NOT trigger the onCreateOptionsMenu. You can override it to do absolutely nothing but then the menu will not be inflated
EDIT - it seems I misunderstood the documentation as well. The key bit for onPrepareOptionsMenu is:
This is called right before the menu is shown, every time it is shown.
Which in practice means that every time you click on the Menu onPrepareOptionsMenu will be called, without calling onCreateOptionsMenu

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

ActionBar.OnNavigationListener fires no matter which item was clicked in ACtionBar

I have a problem.
I have an action bar. It has a drop down list and it has an overflow button and another button some where in between.
Because I use a drop down, I am setting the action bar to use this mode:
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mActionBarNavigationAdapter, mOnNavigationListener);
mActionBar.setDisplayShowTitleEnabled(false);
My mOnNavifationListener is set to call a REST api depending on which item I chose in the drop down list.
This works as excepted.
The problem is, if I click on the overflow button to reveal extra options (such as settings or more importantly, Signout), I still get the navigation listener activated.
So I thought. Ok let's try to distinguish between the buttons using their position or id which are passed as parameters in onNavigationItemSelected method.
So I added an if statement that checks the position parameter. But...
It seems that the position of the overflow button is ALSO 0 (exactly like the position of the first item in the drop down list, so it passes the check and calls the REST api which is not good.
Also, the third button (not the drop down list or the overflow), has a position of 1 which effectively calls the other REST api...
I can't find a way to distinguish between the items in the action bar.
Any suggestions?
First, I'd strongly consider replacing your use of list navigation, as that is officially deprecated in the "L" Developer Preview and should remain deprecated in the next shipping version of Android.
I still get the navigation listener activated
I am not clear on what you mean by this. I also don't know which action bar you are referring to:
native API Level 11+?
appcompat_v7?
ActionBarSherlock?
Ideally, onNavigationItemSelected() will be called when your activity first appears, then if the user chooses something from the Spinner. If you are saying that it is being called at other points in time, while unfortunate, this can be worked around by simply keeping track, in your own data member, of where you are in the Spinner, and only doing work if the Spinner position actually has changed. So, for example, if you start off showing position 0, and the user taps something which causes onNavigationItemSelected() to be called again with position 0, you know that nothing needs changing, because you are still on position 0.

Disabling items in a Navigation Drawer in Android

I'm using the Navigation Drawer Example downloadable here.
I'd like to extend this example to disable the selection of one of the planets. For simplicity, lets say I want to permanently disable the selection of Saturn, and change the text of Saturn to dark gray, and have it not highlight when a user selects it. (in reality, I would like to disable navigation programmatically when a user has changed certain values on the screen not yet saved to the device).
The closest thing I've gotten to this is to stop the selectItem() method from being called from within the onItemClick click listener, but an issue remains even if I do this - the text of "Saturn" still appears selectable and highlights when a user clicks it.
What portion of the widgets do I need to change to prevent the text of Saturn from being highlighted?
I've tried changing
mDrawerLayout.setClickable(false);
mDrawerList.setClickable(false);
But neither of these options appear to have any affect.
Any suggestions or ideas as to how to approach this problem?
A couple helpful notes:
I'm aware that alternatively I can Set the Drawer Lock Mode to prevent the navigation drawer from being opened, but this is much less intuitive than having grayed out nav actions
Is the more appropriate thing to do here to remove the items from the list that cannot be accessed at this time? Similar to as described in the Remove ListView implementation here? The biggest disadvantage to this is that I would probably have to change the listview to describe why items have been removed from this list temporarily.
I'd like to extend this example to disable the selection of one of the planets
Create your own subclass of ArrayAdapter where you override areAllItemsEnabled() to return false and isEnabled() to return true or false as needed.

Options menu not showing up in Android

My objective is to you use one menu for all activities. For that, I have a base activity which consists of 2 methods: onCreateOptionsMenu() and onOptionsItemSelected(). In onCreateOptionsMenu(), I am creating a menu using MenuInflater.
Further, I have 2 activities which extends the above BaseActivity so that same menu is shown for both the activities. My issue is that, when my first activity is launched, options menu is shown, I move to second activity from the first. In the second activity also, when I press the menu button, I am able to view the menu. After that, using Back key press, I come to first activity again, the menu is also shown up there, But when I move to the second activity thereafter, menu is not shown to me.
Can you please post code of your Base Activity's onOptionsItemSelected and onCreateOptionsMenu?
Anyway, with no code available. And not enough clarity, I assume that the following will work for you...
add #Override
public void onBackPressed() {
finish();
}
to your base activity
Minimum SDK version could be the cause. If you reduce it to 13-, you should probably see the menu show up again. Good article on this subject: POST

Categories

Resources