简体   繁体   English

按后退按钮退出应用程序,而不是在 WebView 中向后导航

[英]Pressing back button exits the app instead of navigating backwards in the WebView

I am working on a WebView android app.我正在开发一个 WebView android 应用程序。 I am unable to fix an issue in my app to back navigate.我无法修复我的应用程序中的问题以返回导航。 I am using this code and tried all modifications can be made to this.我正在使用此代码并尝试对此进行所有修改。

public class DeviceActivity extends Activity {
    private WebView web;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        web = (WebView) findViewById(R.id.webView);
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        web.getSettings().setPluginsEnabled(false);
        web.getSettings().setSupportMultipleWindows(false);
        web.getSettings().setSupportZoom(false);
        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);

        // Our application's main page will be loaded
        web.loadUrl("file:///android_asset/fbp/index.html");

        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            public void onBackPressed() {
                if (web.canGoBack()) {
                    web.goBack();
                } else {
                    // My exit alert code goes here.
                }
            }
        });
    }
}

This doesn't navigate back but instead, it exits the app.这不会返回,而是退出应用程序。 Any help would be appreciated.任何帮助,将不胜感激。

Add this in your Activity code (not in onCreate() or nested anywhere else)将此添加到您的Activity代码中(不在onCreate()或嵌套在其他任何地方)

@Override
public void onBackPressed() {
    if (web.copyBackForwardList().getCurrentIndex() > 0) {
        web.goBack();
    }
    else {
        // Your exit alert code, or alternatively line below to finish
        super.onBackPressed(); // finishes activity
    }
}

This will navigate back through the WebView history stack until it is empty and then perform the default action (in this case it finishes the activity).这将返回WebView历史堆栈,直到它为空,然后执行默认操作(在这种情况下,它完成了活动)。

You should probably also set your WebViewClient to have shouldOverrideUrlLoading return true or you won't load any links.您可能还应该将您的 WebViewClient 设置为让shouldOverrideUrlLoading返回true否则您将不会加载任何链接。

In your code you have kept the following block in onCreate() method.在您的代码中,您在 onCreate() 方法中保留了以下块。

if (web.canGoBack()) {
        web.goBack();       
}
else {
    //My exit alert code goes here.    

}

Instead keep the same code in onBackPressed() like this而是像这样在 onBackPressed() 中保留相同的代码

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

also refer toGetting Started: WebView-based Applications for Web Developers in Android另请参阅入门:Android 中面向 Web 开发人员的基于 WebView 的应用程序

We can use webview method to navigate forward and backward.我们可以使用 webview 方法来向前和向后导航。 method boolean canGoForward() for Gets whether this WebView has a forward history item.方法 boolean canGoForward() for 获取此 WebView 是否具有转发历史记录项。 canGoBack() for Gets whether this WebView has a back history item. canGoBack() for 获取此 WebView 是否有历史记录项。 goBack() Goes back in the history of this WebView and goForward() Goes forward in the history of this WebView. goBack()在这个 WebView 的历史中回溯, goForward()在这个 WebView 的历史中前进。 this is my implementation :这是我的实现:

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.forward_btn:
                if (mWebView.canGoForward()){
                    mWebView.goForward();
                }else{
                    Toast.makeText(getApplicationContext(), "Tidak ada halaman lagi!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.backward_btn:
                if (mWebView.canGoBack()){
                    mWebView.goBack();
                }else{
                    Toast.makeText(getApplicationContext(), "Tidak bisa kembali!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.reload_btn:
                layoutLoading.setVisibility(View.VISIBLE);
                mWebView.reload();
                break;
            case R.id.back_btn:
                onBackPressed();
                break;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用导航组件时按下后退按钮退出应用程序而不是导航到上一个屏幕 - Pressing back button exits the app instead of navigating to the previous screen while using navigation component Webview上的“后退”按钮退出应用程序,而不显示先前的Listview结果 - back button on webview exits app instead of displaying previous listview results Android 应用程序在按下后退按钮时退出而不是导航到上一个屏幕 - Flutter - Android App Exits on Press on Back Button Instead of Navigating to Previous Screen - Flutter 应用程序退出,而不是导航到WebView中的上一页 - Application exits instead of navigating to previous page in WebView “后退”按钮退出应用程序,而不是返回MainActivity - Back button exits the app instead of going back to MainActivity Android webview中的ReactJS应用程序 - 按后退按钮关闭应用程序 - ReactJS app in Android webview - Pressing back button closes app 为什么在cordova内部按下后退按钮WebView会关闭应用程序? - Why pressing back button inside of cordova WebView closes app? 后退按钮退出应用程序而不是转到上一个活动 - back button exits app instead of going to previous activity 按下“后退按钮”以返回Web视图 - Go back in webview by pressing “back button” 按下后退按钮后,不进入主要活动,而是关闭应用程序 - After pressing back button, not coming to main activity instead closing the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM