How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the method onBackPressed() in whatever Activity you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
You can suppress user "back" action with onKeyDown() in your activity like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//do actions like show message
return false;
}
return super.onKeyDown(keyCode, event);
}
Related
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);
}
Why when I press this button, a pop up (with text Settings) is displayed in my app ?
How can I remove it?
For handling this keys you need to override this method of the Activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode ==KeyEvent.KEYCODE_MENU) {
//return true if you want block button menu
return true;
}
return super.onKeyDown(keyCode, event);
}
I have two activity let A and B. A is listActivity and B is webViewActivity. Now in webview, when i back pressed then it works fine within webview but in last back pressed, it resides in activity B but I want to go back to Activity A (listActivity).
My code for back pressed
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks.
This may help you...
http://rickluna.com/wp/2014/04/disable-android-back-button-in-inappbrowser/
here it explains that you can disable back button in webview... so make this works in your logic.
#Override
public void onBackPressed() {
super.onBackPressed();
}
onbackpressed function call when you pressed back button then finish current activity.
My requirement is that when user clicks the home button at any screen in application, he is redirected to device home screen and when he comes back to the application he will redirect to app home screen rather than at screen where he pressed the home button.
Any help would be appreciaed.
As you said that you want to go Home Screen when you press Home button from any Screen. So you better take a BaseActivity extending Activity and inside that override onKeyDown() as below.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
// Implement starting Home Activity with Clear Top
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Then Extend all your activities with BaseActivity instead of Activity. So that Every Activity get onKeyDown() Functionality so that any where you press Home Button it navigates back to HomeScreen with removing all Activities.
Here is some code you can use to detect Home-Button pushes and call appropriate functions.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or
In android manifest do setting android:noHistory="true" on the activity,this will remove an activity from the stack whenever it is not there in foreground.
Can I ignore the previous stage of application after tapping Home button in android?
So that I can get the application from initial screen.
Use like this.If now you are in LastActivity and you want to go back to the First Activity and there is more activity in between then if you want to go first from last use
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(autoSearch)
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.d("click","HOMEButtonclicked");
LastActivity.this.startActivity(new Intent(LastActivity.this, FirstActivity.class));
moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}