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
Related
I have an activity with search bar. On start, keyboard shows up.
If I hit back button, it dismisses the keyboard. I need to hit back button 3 times to dismiss activity. The first hit dismisses keyboard, second hit loses focus from search bar.
How to dismiss activity using back button?
You can catch back event and add your own destination activity as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// finish current activity ( finish(); )
// add your new intent here
// Example:
// startActivity(new Intent(<your_context>, <destination_activity>));
}
return super.onKeyDown(keyCode, event);
}
i want to disable navigation bar in my app.i have use below code for it.it disable back button.now i want to disable home button when click on toggle button.how to disable home button using toggle button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//preventing default implementation previous to
//android.os.Build.VERSION_CODES.ECLAIR
return false;
}
return super.onKeyDown(keyCode, event);
}
You can't disable the home button.
But there is a workaround: Start your app again as soon as another app is launched.
See http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
I have a non-application window (i.e. a window with no activity context, that appears over other apps). This window is technically a View which has been added using WindowManager.addView() and TYPE_SYSTEM_ERROR.
I would like to remove this window when the Home button is pressed but it seems impossible to intercept the Home button press.
Is there any way to do this?
EDIT: My app does not have an Activity so cannot use any methods on the Activity class.
You can detect when a user press Home button with:
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
but you can't override system behaviour for that action.
Below method used for enabling Home button key press event
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
//Leave Blank no code like back press method so by pressng home button it will not go to home
//This can be used for long press home button also
}
return true;
}
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.
I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity