簡體   English   中英

返回時,選項卡空白中的WebView

[英]WebView in a tab blank when go back

好吧,當我顯示一條信息時,我在選項卡中有一個Web視圖。 問題是,當我更改片段(我正在使用該片段)並返回時,Web視圖為空白(黑色)。

我正在使用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 Fragment:

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的狀態並在用戶導航回該狀態時將其還原。

這是示例代碼片段。 您應該將此代碼段放入使用WebView Fragment中。

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

然后在片段的onCreateView或onViewCreated方法中將其還原,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM