Webview后退按钮不会返回

时间:2015-07-18 12:24:34

标签: android webview

请帮助 当您按后退Button退出应用程序时 我尝试了很多解决方案,但没有成功 我会把你的代码

MainActivity

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity{

    WebView webview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.activity_main);

        webview = (WebView) findViewById(R.id.webView);

        webview.getSettings().setJavaScriptEnabled(true);


        webview.getSettings().setBuiltInZoomControls(false);

        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);

        webview.loadUrl("http://www.alrofaiy.com/");

        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                setProgress(progress * 100);
            }
        });


        webview.setWebViewClient(new InsideWebViewClient());

    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();

        if(webview.canGoBack()){
            webview.goBack();
        }

    }
    private class InsideWebViewClient extends WebViewClient {
        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;

        }

    }



    @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.


        switch (item.getItemId()){
            case R.id.item1:
                try {
                    Intent abb = new Intent(Intent.ACTION_SEND);
                    abb.setType("text/palin");
                    abb.putExtra(Intent.EXTRA_SUBJECT , "APP");
                    String aliwi = "/n    /n/n";
                    aliwi = aliwi + "https://    /n/n";
                    abb.putExtra(Intent.EXTRA_TEXT, aliwi);
                    startActivity(Intent.createChooser(abb,""));

                }
                catch (Exception o)
                {o.toString();}
                return true;
            case R.id.item2:

                Intent AAA = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:info@alrofaiy.com?subhect= "));
                startActivity(AAA);
                return true;
            case R.id.link1:
                webview.loadUrl("http://www.alrofaiy.com/login_in");

                return true;

        }

        return super.onOptionsItemSelected(item);
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:padding="0dp">


    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:focusableInTouchMode="false"
        android:padding="0dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/text_call"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/webView"
        android:layout_alignEnd="@+id/webView"
        android:autoLink="phone"
        android:clickable="true"
        android:linksClickable="false"
        android:textColor="#fff"
        android:background="#000"
        android:paddingTop="2dp" />

</RelativeLayout>

解决方案是什么? 当您运行应用程序并查看该站点时,按下后退按钮时会出现

1 个答案:

答案 0 :(得分:1)

@Override
public void onBackPressed() {
    super.onBackPressed();

    if(webview.canGoBack()){
        webview.goBack();
    }

}

应该是

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