Android onBackPressed问题

时间:2016-12-12 09:32:50

标签: android webview onbackpressed

我按照网络上的说明创建了一个基本的webview应用程序。 一切顺利,除了背景按钮,当连接到服务器背景的webview进入主屏幕,但当我点击菜单项(搜索和aboutpage)中的主页时,应用程序关闭。 这是我的代码

MainActivity.java

package com.dijitalyayincim.cekmekoy.cekmekoy_web;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private boolean isRedirected;



    private ProgressBar progress;

    String url = "file:///android_asset/www/index.html";
    String url1 ="http://www.cekmekoy.info/index.php?option=com_sppagebuilder&view=page&id=12";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //setContentView(R.layout.aa);



       webView = (WebView) findViewById(R.id.webView);
        startWebView(webView, url);

    }

    @Override
     public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;

            case R.id.action_info:
                Element adsElement = new Element();
                adsElement.setTitle("Advertise with us");

                View aboutPage = new AboutPage(this)
                        .isRTL(false)
                        .setImage(R.drawable.logo_cekmekoy)
                        .addItem(new Element().setTitle("Version 1.0"))
                        .addItem(adsElement)
                        .addGroup("Bize Ulaşın")
                        .addEmail("info@cekmekoy.info")
                        .addWebsite("http://www.cekmekoy.info/")
                        .addFacebook("cekmekoyinfo")
                        .addTwitter("cekmekoyinfo")
                        .addYoutube("UCYt3Z595jctz5oUhg0SMLqQ")
                        .addInstagram("cekmekoy_info")
                        .create();

                        setContentView(aboutPage);
                setTitle("Bize Ulaşın");
                        return true;


            case R.id.action_search:

                setContentView(R.layout.test);
              webView = (WebView) findViewById(R.id.search);
               startWebView(webView, url1);
                setTitle("Arama");



            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);

        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    private void startWebView(WebView webView,String url) {

        webView.setWebViewClient(new WebViewClient() {
            ProgressDialog progressDialog;


            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    view.reload();
                    return true;
                }
                view.loadUrl(url);
                isRedirected = true;
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                isRedirected = false;
            }

            public void onLoadResource (WebView view, String url) {
                if (!isRedirected) {
                    if (progressDialog == null) {
                        progressDialog = new ProgressDialog(MainActivity.this);
                        progressDialog.setMessage("Yükleniyor...");
                        progressDialog.show();
                    }
                }

            }
            public void onPageFinished(WebView view, String url) {
                MainActivity.this.setTitle(view.getTitle());
                try{
                    isRedirected=true;

                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }



                }catch(Exception exception){
                    exception.printStackTrace();
                }
            }

        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);
    }


    @Override
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

如果你能提供帮助我真的很感激

感谢...

2 个答案:

答案 0 :(得分:0)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

使用此代码。希望能帮到你。

答案 1 :(得分:0)

评论此终点线并使用您的方法。 webView.goBack();

  @Override
 public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
 //                finish();
            return true;
 }
}