首页 > 解决方案 > 返回时选项卡中的 WebView 空白

问题描述

好吧,当我显示一条信息时,我在选项卡中有一个 webview。问题是当我更改片段(我正在使用它)并返回时,webview 是空白的(黑色)。

我正在使用loadDataWithBaseURL方法,并且尝试填充historyUrl参数,但得到了相同的结果。

这是我的一段代码:

String html ="<html><head>" +
            "<style type=\"text/css\">"+
            "@font-face { font-family: '"+ AppData.getFont(context)+"'; src: url('"+AppData.getFont(context)+"'); }"+
            "* {font-family: '"+AppData.getFont(context)+"'; font-size: "+font+" !important; font-weight:normal !important;}"+
            "h1 {font-size: "+h1+" !important;}"+
            "strong, b {font-family: '"+AppData.getFont(context)+"'; font-weight: bold !important;}"+
            "</style>"+
            "</head><body>",
            ending="</body></html>";

    Log.e("HTML", html);
    String filename = "file:///android_asset/";

    String myHtmlString = html + product.getCurrentDescription(context) + ending;
    tvDescription.loadDataWithBaseURL(filename, myHtmlString, "text/html; charset=utf-8", "utf-8", filename);

这是我用来管理选项卡的方法:

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tvTabDescription = (TextView) inflater.inflate(R.layout.custom_tab_text,null);
    TextView tvTabCondition = (TextView) inflater.inflate(R.layout.custom_tab_text,null);
    TextView tvTabLocation = (TextView) inflater.inflate(R.layout.custom_tab_text,null);

    //Creating custom tabs
    tvTabDescription.setText(context.getResources().getString(R.string.tab_description));
    tabLayout.addTab(tabLayout.newTab().setCustomView(tvTabDescription));

    tvTabCondition.setText(context.getResources().getString(R.string.tab_conditions));
    tabLayout.addTab(tabLayout.newTab().setCustomView(tvTabCondition));

    tvTabLocation.setText(context.getResources().getString(R.string.tab_location));
    tabLayout.addTab(tabLayout.newTab().setCustomView(tvTabLocation));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final PagerAdapter pagerAdapter = new com.iurban.iurbanapp.Adapters.PagerAdapter(getFragmentManager(),tabLayout.getTabCount(), product, context);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            if(tab.getPosition() == 2)
                ivProduct.setVisibility(View.GONE);
            else
                ivProduct.setVisibility(View.VISIBLE);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

我要分享这两张图片,第一张是第一次到达这个部分,第二张是点击“Reservar”按钮后返回。

在此处输入图像描述

在此处输入图像描述

当我单击“Reservar”按钮时,它会加载 FormFragment 片段:

formFragment = new FormFragment();
    fragmentManager.beginTransaction()
            .replace(R.id.fm_container, formFragment)
            .addToBackStack(CustomConstants.FORM).commit();

并且返回执行调用 super.onBackPressed() 的 onBackPressed() android 方法;

我的 FormFragment 的 onCreate 看起来像下一段代码,这两种方法都只执行“初始化组件(从 xml 获取视图)”和“为此组件初始化方法 onClick”:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_form, container, false);
    initComponent(rootView);
    initOnClicks();
    return rootView;
}

先感谢您。

标签: androidandroid-fragmentswebviewtabs

解决方案


我不确定我是否正确理解了您的问题,但是您可以保存 WebView 的状态并在用户导航回它时恢复它。

这是示例代码片段。您应该将此代码片段放入您Fragment使用的WebView.

@Override
protected void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState);
    super.onSaveInstanceState(outState);
}

并在您的片段的 onCreateView 或 onViewCreated 方法中恢复它,如下所示:

if (savedInstanceState != null){
   webview.restoreState(savedInstanceState);
}
else{
   webview.loadUrl(url)
}

推荐阅读