android - 返回时选项卡中的 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;
}
先感谢您。
解决方案
我不确定我是否正确理解了您的问题,但是您可以保存 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)
}
推荐阅读
- augmented-reality - Google Model Viewer AR-如何在没有海报或对象预览的情况下拥有“在 AR 中查看”按钮
- compiler-errors - 如何从 arrayList 中的不同对象调用方法?
- node.js - 错误:无法加载默认凭据
- java - 重复发送 HTTP 请求,即使应用程序在 Android 上处于后台也是如此
- html - 我可以在引导程序上编辑多个类吗?
- graphql - SubSelectionRequired 类型的验证错误:字段的类型为空需要子选择
- javascript - 时刻 - 从星期几计算日期
- java - 使用 JAAS 进行用户验证的最佳场所
- sql - 在PostgreSQL中将两个或多个不同的SELECT查询组合到具有不同条件的同一张表中
- java - 如何查看存储在 ArrayList 中的所有元素?