3

I have a mirroring of a website in WebView and I would like that when I press the ** Back ** button on the Smartphone, I would go back to the previous page. Currently if I press the ** Back ** button on the Smartphone, the application is minimized (It stays in the background). I do not know how to do this, can anyone help me?

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

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

        WebView wv = (WebView) findViewById(R.id.webview1);
        wv.loadUrl("https://www.xxxx.com.br");
        wv.setWebViewClient(new WebViewClient());

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);

Manifest

<uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_stat_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


            </intent-filter>
        </activity>
    </application>

</manifest>
Ever
  • 73
  • 2
  • 9
  • Hi, I'm new to the community. Sorry, I did not know how to use the search feature. What is the procedure for deleting this topic, since the topic you posted helped in solving the problem: How to go back to previous page if pressed in WebView? – Ever Oct 18 '17 at 04:56

2 Answers2

8

Override the onBackPressed method in your activity as below:

@Override
public void onBackPressed(){
    WebView wv = (WebView)findViewById(R.id.webview1);
    if(wv.canGoBack()){
        wv.goBack();
    } else {
        super.onBackPressed();
    }
}

Use the above method inside your class, not outside.

public class MainActivity extends AppCompatActivity {

    //onCreate method

    //onBackPressed method

    //other methods

}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Use this .

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Add wv as global data

private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv = (WebView) findViewById(R.id.webview1);
    wv.loadUrl("https://www.xxxxx.com.br");
    wv.setWebViewClient(new WebViewClient());

    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setSupportZoom(false);

}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42