Doubt simple back web view

Asked

Viewed 427 times

0

I’m creating a simple app on android studio 3.0, but I don’t 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(); 
   } 
}

2 answers

1

You will have to override some back button method, I would recommend the onBackPressed, but I also found this reply in the English OS showing a overridein the keydown

It would look something like this:

@Override
public void onBackPressed()
{
    if (webView.canGoBack())
    {
        webView.goBack();
    }
    else
    {
        finish();
    }
}
  • Answer containing only link are not very interesting, could put a minimum use example?

1


Has the 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 the método "test" by botão in his 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>
  • I used this, but I received 3 errors, Error:(47, 9) error: class, interface, or Enum expected

  • You didn’t copy and paste everything, right? Because you have to know the name of your class and change the code !

  • I Colei and changed the ones that were in red, example the Activity was testing I put mine which is main

  • Yes, but that’s the webview Class... you have to call her, not the main one. I’ll paste all the code.

  • Okay, I’ll just try for a minute

  • Remembering that your layouts have to be hitting the ID’s with the TEST class, if it doesn’t already give error when calling the webview

  • Raoni, I couldn’t, there’s some kind of chat here?

  • https://chat.stackexchange.com/rooms/68026/onkeydown

  • You must have 20 reputation on The Stack Exchange Network to chat here. See the Faq . I can’t answer you

  • You have skype?

  • Yeah, give me yours

Show 7 more comments

Browser other questions tagged

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