Simple doubts back web view

0

I'm creating a simple application in android studio 3.0 , but I do not know anything about programming and with a lot of effort I was able to add the webview and display my page.

But when I click the back button the application closes, I searched the internet and tried to use a command line webview.cangoback but the letters turn red and I have no idea how to do it later, I hope someone can help me.

@Override public void onBackPressed() { 
   if (this.webView.canGoBack()) { 
       this.webView.goBack(); 
   } else { 
        this.finish(); 
   } 
}
    
asked by anonymous 01.11.2017 / 17:57

2 answers

1

It has onKeyDown

Class MainActivity:

public class MainActivity extends AppCompatActivity {

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

    public void teste(View view) {
        Intent i = new Intent(this, Teste.class);
        startActivity(i);
    }
}

You will call método "test" by botão on your MainActivity

Class Teste:

public class Teste extends AppCompatActivity {

    private WebView wv;
    private WebSettings ws;
    private WebViewClient wc;

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

        this.wv = (WebView) findViewById(R.id.webViewTeste);
        this.wc = new WebViewClient();
        this.ws = this.wv.getSettings();
        this.ws.setJavaScriptEnabled(true);
        this.ws.setSupportZoom(true);
        this.ws.setBuiltInZoomControls(true);
        this.ws.setDisplayZoomControls(false);
        this.wv.setWebViewClient(wc);
        wv.loadUrl("http://www.seusite.com.br");
    }

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

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6ee9c"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.susite.app.MainActivity"
    android:weightSum="1">

    <Button
        android:id="@+id/btnEntrar"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:onClick="teste"
        android:text="entrar" />

</LinearLayout>

activity_teste.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.seusite.app">

    <WebView
        android:id="@+id/webViewTeste"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
    
01.11.2017 / 18:18
1

You will have to override some method of the back button, I would recommend onBackPressed , but I also found this response in the English OS that shows a override in keydown

It would look something like this:

@Override
public void onBackPressed()
{
    if (webView.canGoBack())
    {
        webView.goBack();
    }
    else
    {
        finish();
    }
}
    
01.11.2017 / 18:12