Android onBackPressed issue



我从Web创建了一个基本的WebView应用程序。一切正常,除了" backpress"按钮,当连接到服务器backpress的WebView到主屏幕时,但是当我在菜单项中单击" home"(搜索和大约页面)时,应用程序已关闭。这是我的代码

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();
        }
    }
}

我真的很感激您是否能提供帮助

谢谢...

@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);
}

使用此代码。希望帮助您。

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

  @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
 //                finish();
            return true;
 }
}

相关内容

  • 没有找到相关文章

最新更新