How to disable back button pressed for webview in android ?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (wv1 != null && (keyCode == KeyEvent.KEYCODE_BACK)
&& wv1.canGoBack() )
{
wv1.goBack();
}
return true;
}
If you want to disable back button action when the WebView Visible and enable back button action if the WebView in not Visible try the below code in your Activity
#Override
public void onBackPressed() {
if(webview.getVisibility()==View.VISIBLE){
// dont pass back button action
if(webview.canGoBack()){
webview.goBack();
}
return;
}else{
// pass back button action
super.onBackPressed();
}
}
Simply override the onBackPressed() method.
#Override
public void onBackPressed() { }
Please try this
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
}
else{
super.onBackPressed();
}
}
There are many ways to make it,
Solution 1, overriding dispatchKeyEvent()
dispatchKeyEvent()(API Level 1, Android 1.0)
Refer to my answer use dispatchKeyEvent to disable back button
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.dispatchKeyEvent(event);
}
Solution 2, overriding onBackPressed()
onBackPressed() (API Level 5, Android 2.0)
Refer to Use onBackPressed() to disable back button
#Override
public void onBackPressed() {
}
Solution 3, overriding onKeyDown()
onKeyDown() (API Level 1, Android 1.0)
Refer to Use onKeyDown() to disable back 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 true;
}
return super.onKeyDown(keyCode, event);
}
You have add below code in Activity for disable activity back pressed
#Override
public void onBackPressed() {
}
Add this below code in java file :
WebView mwebView;
Button backButton1;
backButton1 = findViewById(R.id.backButton1);
mwebView = findViewById(R.id.mwebView);
backButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWebView.destroy();
}
});
Related
I want to change the functionality of back button for a particular bunch of code being active. Once its done how do i reset it to default.
I am using following piece of code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode== KeyEvent.KEYCODE_BACK && issearchopen)
{
searchView.closeSearch();
}
else{
}
return true;
}
boolean issearchopen is true whenever my 'bunch' of code is active.
Thanks in advance
It should look like this
#Override
public void onBackPressed() {
if(isSearchOpen){
// do stuff
} else {
super.onBackPressed(); // default behaviour
}
}
To do it you need to override onBackPressed() method.
Your code will looks like this
#Override
public void onBackPressed() {
if(issearchopen){
searchView.closeSearch();
issearchopen = false;
} else {
super.onBackPressed(); // default back press behaviour
}
}
Also don't forget to change issearchopen to true when open searchView.
I use simple sidebar drawer and i want when user back pressed if drawer open close and when close activity finish. This library have not this function default. I use below code but not work :(
final SimpleSideDrawer mSlidingMenu = new SimpleSideDrawer( mactivity );
mSlidingMenu.setLeftBehindContentView(R.layout.sidebar);
mSlidingMenu.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mSlidingMenu.closeLeftSide();
return true;
} else
return false;
}
});
toggleDrawer() method can be used to toggle between opening and closing of the drawer. So pair it up with onBackPressed() method to close the drawer while pressing back button.
#Override
public void onBackPressed() {
mSlidingMenu.toggleDrawer();
}
#Override
public void onBackPressed() {
if(mSlidingMenu.isClosed())
{
super.onBackPressed();
}
else
{
mSlidingMenu.closeLeftSide();
}
}
i am trying to disable the back button in my device with the following code.
the code is working but i would like that the function that handle all the back button request in the fragments will derived from the Main Activity
this is the back button handler:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getActivity(), "Please navigate via the menu", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
});
}
It's easier to override onBackPressed function:
private boolean disabled = true;
#Override
public void onBackPressed() {
if (!disabled) {
super.onBackPressed();
}
}
With this you can easy change disable flag and enable back button when needed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
//leave it empty
}
return super.onKeyDown(keyCode, event);
}
It's generally not the best idea to disable the back button as the user expects that button to perform some sort of back navigation. Now that being said, if you have a specific reason you can disable the back button by overriding the onBackPressed at the activity level and not calling into super.
Put this code at your MainActivity:
#Override
public void onBackPressed() {
executeAtAndroidOnBackPressed();
}
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
executeAtAndroidOnBackPressed();
}
return false;
}
protected void executeAtAndroidOnBackPressed() {
// do nothing
}
I want to close application when I press device's back button.I am using this code..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the onBackPressed() in your Activity where you want application to Quit when the device back button clicked
#Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This code does it :
public void onPause() {
super.onPause();
finish();
}
Don't forget to mark it as answer if it helped ;)
The answer that you marked as correct will stop the app every onPause of this activity, which is not an event of the back button cick, the right answer is :
#Override
public void onBackPressed() {
finish();
}
Is it possible to change this code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return super.onKeyDown(keyCode, event);
}
so that when I click on back button it works like a home button?
try this,
#Override
public void onBackPressed() {
Intent backtoHome = new Intent(Intent.ACTION_MAIN);
backtoHome.addCategory(Intent.CATEGORY_HOME);
backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(backtoHome);
}
Adding this to your Activity, will make it look like your app is responding to a Home Button Click event
One line solution
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
And also see this
#Override
public void onBackPressed()
//super.onBackPressed();
NavUtils.navigateUpFromSameTask(this);
}
If something doesn't work like expected try it with uncommented
super.onBackPressed();