如何从上一页返回

时间:2017-12-20 16:52:18

标签: android

所以我想从之前的每个时间回来我按下后退按钮它会将我重定向到主页我想知道是否有人可以帮我解决这个问题,这是我尝试在我的代码中实现的地方。

public class DirectoryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_directory);

        String url = "http://www.example.com";


        final WebView mWebView = (WebView) findViewById(R.id.webView7);
        WebView web = (WebView) findViewById(R.id.webView7);
        web.loadUrl(url);


        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mWebView.getSettings().setSavePassword(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setSaveFormData(true);
        mWebView.getSettings().setSupportZoom(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setSupportMultipleWindows(false);
        mWebView.getSettings().setLightTouchEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        mWebView.setWebViewClient(new WebViewClient() {


            public void onBackPressed() {
                if(mWebView.canGoBack())
                {
                    mWebView.goBack();
                }

                else
                {
                    DirectoryActivity.super.onBackPressed();
                }
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return true;
            }
        });
        mWebView.loadUrl("http://www.example.com");

    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码..

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        return;
    }
    super.onBackPressed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return false;
}