The problem of pressing back to exit when android uses webview

Use webview to package html5 games, the code is all good, as follows

 

public class MainActivity extends ActionBarActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         WebView webView=new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);//支持js
webView.loadUrl("http://www.gg4493.com/");
        webView.setWebViewClient ( new MyWebViewClient ());


    }


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

        //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 

Everything seems to be perfect, I played a game and was defeated by the computer, click "More Games" to view other games, and then click the back button, the problem is, which is not the best mining technology, but the webview does not have a back function, so I can't return to the home page , but quit directly, there are so many problems in android development, fortunately, brother is clever, looking for information!

Finally found the problem,

Just rewrite the activity's onKeyDown method and go back in onKeyDown

public boolean onKeyDown(int keyCode ,KeyEvent keyEvent){
if(keyCode==keyEvent.KEYCODE_BACK){//Listen to the return key, if you can go back, go back
if(webView.canGoBack()){
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, keyEvent);

}

 

All code is posted, so as not to forget later

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {
private WebView webView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


private void init() {
webView = new WebView(this);
 webView.loadUrl("http://www.mm4493.cn/");
 WebSettings settings = webView.getSettings();
 settings.setJavaScriptEnabled(true);
 webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
  view.loadUrl(url);
return true;
}

});
setContentView(webView);

public boolean onKeyDown(int keyCode ,KeyEvent keyEvent){
if(keyCode==keyEvent.KEYCODE_BACK){
if(webView.canGoBack()){
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, keyEvent);

}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326072388&siteId=291194637