android onPrepareOptionsMenu doen't get second press on menu button - android

I notice that when implementing onPrepareOptionsMenu() in my activity - the first press works good - the callback from onPrepareOptionsMenu() starts. but when it still visible(open some dialog) and I want the second press to close it(trigger the callback to close the dialog) - the second press on the menu button doesn't triggers onPrepareOptionsMenu(). don't sure why
This is how I implement it:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (onDoneListener != null) {
onDoneListener.OnDone();
}
return false;
}
EDIT1:
I added the next function after the commenter help but still no luck. The OnKeyDown() also not receiving the next menu button press. It looks like the menu button doesn't get events until I'm pressing the back button. Here is the code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (onDoneListener != null)
onDoneListener.onDone();
}
return super.onKeyDown(keyCode, event);
}
I also tried to return true and false but not luck.

What you are seeing is the exact intended behavior of OnPrepareOptionsMenu. It runs before the menu is shown, not after.

Related

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Android, handle onBackPressed while drawer is showing

In default, when the drawer is showing and you press the BackButton it automatically close.
but I want when it's showing and user clicks on BackButton, close the app.
I tried these solution but didn't work :
Navigation drawer, handling the back button to go to previous fragments
Backbutton press from android navigation drawer
onBackPressed will not be called on drawer open or lets say if your keyboard is open then also your back key changes to down arrow. So when you press that down key your keyboard hides and your onBackPressed will not be called still.
Your app will be closed when you press the back button again. Same is the case with keyboard.
So first close the drawer, then press back button again, and then your app will finish
First override back pressed of your activity and then can check if drawer is open
mDrawerLayout.isDrawerOpen(GravityCompat.START);
If this return true simply close your app.
Important : Check your drawer gravity.
Try registering you fragment to back pressed like this:
public void registerBackButton()
{
if (getView() != null)
{
getView().setFocusableInTouchMode(true);
getView().requestFocus();
final BaseFragment frag = this;
getView().setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() != KeyEvent.ACTION_DOWN)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (isVisible())
{
return frag.onFragmentBackPressed();
}
}
}
return false;
}
});
}
}
Now in onFragmentBackPressed check if drawer is opened or not as mentioned above.

Android: Finish activity when SlidingMenu (jfeinstein) is showing

I'm trying to implement the sliding menu created by jfeinstein in my app. The behavior I want to achieve is that when I press the back button and the menu is not showing, it will show up. This works great.
However when the menu is showing pressing the back button should finish the current activity. What happens instead is that the sliding menu is just closed again.
To achieve the described behavior I have overwritten onBackPressed:
#Override
public void onBackPressed()
{
SlidingMenu sm = getSlidingMenu();
if(!sm.isMenuShowing())
{
sm.showMenu();
}
else
{
finish();
}
}
But as soon as the sliding menu is showing onBackPressed isn't called anymore. I suspect as I have to use a SlidingFragmentActivity pressing the back button triggers the fragment history stack to pops.
Does anybody know how to solve this problem?
After browsing the SlidingMenu code a little further I found that onKeyUp is overwritten in SlidingFragmentActivity. So the way to go is to override onKeyUp in your activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
SlidingMenu sm = getSlidingMenu();
if(sm.isMenuShowing())
{
finish();
return true;
}
}
return super.onKeyUp(keyCode, event);
}

Android : override BackButton when menu is visible

Let's say I have an activity A that overrides the back button to show some dialog and that this activity has a menu.
So, when the back button is pressed the dialog shows up, but if the user press the menu button and then the back button, the dialog isn't shown. How can I make the behavior for the back button be the same whether the menu is visible or not?
You need to override the BackButton.
onBackPressed()
{
closeOptionsMenu(); // to close the Options Menu if it is visible
//your code here
}
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return true;
}
hope this help

Android: show white background when search button pressed

When I press the search key on my device, I want it to show a white background. However, when I press the back button, I want the previous activity's background to be restored. ivBackground is a variable I added to my relativelayout which I turn VISIBLE to show the white background.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. It only goes away if I press the back button once again. Any solutions?
On the relevant activity, you can get a reference to a SearchManager object. On this, you can set an OnDismissListener, which is called the when search UI is dismissed e.g.
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested inside your activity class, which is called when a user signals the desire to start a search
#Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? This seems like the right solution anyway since the two events are logically connected. If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view.

Categories

Resources