Android - Webview button back

Asked

Viewed 1,140 times

0

I have a mirroring of a website on Webview and would like to press the button Come back back to the previous page. Currently if I press the button Come back from Smartphone, the application is minimized (Stays in the background). I don’t know how to do it, someone can 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>

1 answer

0


You must use the function onKeyDown, 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);
}

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

For it to work, you should declare Webview up there first, example:

private WebView wv;

And then within the onCreate declares it, example:

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

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

And if there are no more pages to return, it will fall on Else and then it will close the Activity


Edit: In your mainactivity, it would look that way:

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);
}

}

  • Please, you can assemble the script based on my Mainactivity, reason: Error presented (I am layman in programming).

  • I edited my answer with the code of how it would look in your Mainactivity, give a look, any questions warns

  • After the edition I was successful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.