android - using phones back button to exit from webview -


the phone button works in webview,but when want exit webview application didn't anything. put button go application did't remain way.

java:

package com.wiralss.adb;   import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.keyevent; import android.view.view; import android.view.view.onclicklistener;  import android.view.window;  import android.webkit.webchromeclient;  import android.webkit.webview; import android.webkit.webviewclient; import android.widget.button;  public class extends activity implements onclicklistener{  webview webview;    final activity activity = this;  @override public void oncreate(bundle savedinstancestate) {   super.oncreate(savedinstancestate); this.getwindow().requestfeature(window.feature_progress); setcontentview(r.layout.instagrem); webview = (webview) findviewbyid(r.id.webview1); button b123=(button)findviewbyid(r.id.button1235); b123.setonclicklistener(this); webview.getsettings().setjavascriptenabled(true); webview.loadurl("https://www.google.com");  webview.setwebchromeclient(new webchromeclient() {     public void onprogresschanged(webview view, int progress)     {         activity.settitle("loading...");         activity.setprogress(progress * 100);          if(progress == 100)             activity.settitle(r.string.app_name);      } });   webview.setwebviewclient(new webviewclient() {     @override     public void onreceivederror(webview view, int errorcode, string description,    string failingurl)     {         // handle error      }       @override     public boolean shouldoverrideurlloading(webview view, string url)     {         view.loadurl(url);         return true;     } });    }    @override public void onclick(view v) {     switch(v.getid()){      case r.id.button1235:         intent b = new intent(this, mainactivity.class);     startactivity(b);     break;      // todo auto-generated method stub  }      // todo auto-generated method stub  } public void onbackpressed (){      if (webview.isfocused() && webview.cangoback()) {             webview.goback();            }     else {             super.onbackpressed();             finish();     } } @override public boolean onkeydown(int keycode, keyevent event) {     if (keycode == keyevent.keycode_back) {         webview.goback();           return true;     }     return false; } } 

xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <webview         android:id="@+id/webview1"         android:layout_width="match_parent"         android:layout_height="match_parent"          />      <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:orientation="vertical" >          <button             android:id="@+id/button1235"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_margintop="580px"             android:layout_weight="0.05"             android:text="button" />      </linearlayout>  </relativelayout> 

another question. remove button need delete?:

button b123=(button)findviewbyid(r.id.button1); b123.setonclicklistener(this); 

and?

@override public void onclick(view v) {     switch(v.getid()){      case r.id.button1:         intent b = new intent(this, mainactivity.class);     startactivity(b);     break;      // todo auto-generated method stub  }      // todo auto-generated method stub  } 

tank much.

you don't need call super.onbackpressed() controlling finishing of activity yourself.

equally, don't need webview.isfocused() method call.

you can rewrite onbackpressed method read this:

public void onbackpressed() {     if (webview.cangoback()) {         webview.goback();            }  else {         finish();     } } 

i'm not sure on motives including button1235 button, wanting remove?


Comments