Android - WebView back button

0

I'm mirroring a site in WebView, and I'd like you to go back to the previous page by pressing the Back button on the Smartphone. Currently, if I press the Back button on the Smartphone, the application is minimized (it is 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>
    
asked by anonymous 17.10.2017 / 21:35

1 answer

0

You should use the onKeyDown function, for example:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wv.canGoBack()) {
                    wv.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

In this way, Java will understand that it is to return a webView page that is being displayed.

To work, you must declare WebView up there first, for example:

private WebView wv;

And then, within onCreate declares it, example:

wv = (WebView) findViewById(R.id.webview1);

So you will have access to the wv variable within the onKeyDown

And if there are no more pages to return, it will fall into else and then it will close Activity

Edit: In your MainActivity, it would look like this:

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 {

    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.xxxx.com.br");
        wv.setWebViewClient(new WebViewClient());

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wv.canGoBack()) {
                    wv.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

}

    
17.10.2017 / 21:44