Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

移动用户反馈页面导航问题 #212

Open
DevDengChao opened this issue Jun 14, 2019 · 1 comment
Open

移动用户反馈页面导航问题 #212

DevDengChao opened this issue Jun 14, 2019 · 1 comment

Comments

@DevDengChao
Copy link

背景: 目前我们的应用使用插件化进行开发, 将 feedback 作为单独的一个应用开发完毕后植入宿主的插件管理器中.

现状: MainActivity 负责实例化 Feedback 相关 fragment 并展示后, 如果用户进入二级页面时误触返回键, 会导致 Activity 被关闭, 而不是返回一级页面.

诉求: 提供相关接口用于管理 WebView 的导航.

workaround: 使用反射获取 WebView, 在 onBackPressed() 回调中对 webView 进行状态判断.

@DevDengChao
Copy link
Author

Workaround used:

    @Override
    public void onBackPressed() {
        if (webView == null && fragment != null) {
            // 通过反射获取 WebView 对象, 以便管理导航
            Field[] fields = fragment.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (WebView.class.isAssignableFrom(field.getType())) {
                    field.setAccessible(true);
                    try {
                        webView = (WebView) field.get(fragment);
                    } catch (IllegalAccessException ignore) {
                        // never
                    }
                    break;
                }
            }
        }

        if (webView == null) {
            super.onBackPressed();
        } else {
            if (webView.canGoBack()) {
                webView.goBack();
                if (url == null) {
                    url = webView.getUrl();
                } else {
                    // webView 退到最后一页时, 协助关闭页面
                    if (url.equals(webView.getUrl())) {
                        super.onBackPressed();
                    } else {
                        url = webView.getUrl();
                    }
                }
            } else {
                super.onBackPressed();
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant