0

can you please tell me why the application exit when I click back button of device.I am using webview .I go to one link "one.link" on clicking any on that page it will go to "next.link" then it goes to next page. But when I press back button my application exit.Not go to previous page .It mean it not support history .

can we do that ?

I used this code Layout.xml

<WebView 
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
/>

and ApplicationLoad.java is :

 public class ApplicationLoad extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.app);

            WebView browser = (WebView) findViewById(R.id.webview);
            browser.loadUrl("http://www.tutorialspoint.com");
        }           

    }
Avi
  • 21,182
  • 26
  • 82
  • 121
  • I think because you have called finish() in every activity remove it and try. – InnocentKiller Feb 27 '14 at 07:31
  • 1
    You should take a look at this answer: http://stackoverflow.com/a/6077173/2668136 & http://stackoverflow.com/a/9743841/2668136 – Blo Feb 27 '14 at 07:32

5 Answers5

4

By default Activity is suppose to be finished, so it is default behaviour, in order to do a custom task on back key press event, you need to override this method of your Activity.

@Override
public void onBackPressed() {
    if(browser.canGoBack()){
         //if we can go back, just go back
         browser.goBack();
     }else{
         //let the parent handle it.
         super.onBackPressed();
     }
}

Make sure to make WebView your class field like this:

WebView browser;

and initialize it inside onCreate();

this. browser = (WebView) findViewById(R.id.webview);
browser.loadUrl("http://www.tutorialspoint.com");
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
1
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(mWebView.canGoBack() == true){
                    mWebView.goBack();
                }else{
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0

You need to override the onBackPressed() method and implement history navigation yourself.

Something like this:

@Override
public void onBackPressed() {
    if(browser.canGoBack()) {
        browser.goBack();
    } else {
        super.onBackPressed();
    }
}

This works for Android 2.2+, override onKeyDown() for older Android versions.

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
0

thats how the back button wroks by default. it finishes the current activity. to go back to tge histoy you should override onBackPressed

 @Override
 public void onBackPressed() 
 {

    if (webView.canGoBack()) 
    {
        webView.goBack();
    }
    else
    {
            super.onBackPressed();
    }

 }
null pointer
  • 5,874
  • 4
  • 36
  • 66
0

Copy this snippet to your activity.

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mWebView=(WebView)findViewById(R.id.webview);

    // Do other things
}
@Override
public void onBackPressed() {
    //Check if the webview can go back
    if (mWebView.canGoBack()) {
        mWebView.goBack();
    }else{
    //This will finish the activity
    super.onBackPressed();
    }
}
Riki
  • 2,774
  • 1
  • 24
  • 38