android – 更改片段并保存Web视图状态 – 变得疯狂

android – 更改片段并保存Web视图状态 – 变得疯狂,第1张

概述我一直试图在一周的最佳时间做这件事但仍然没有运气. 我有一个带有标签的 *** 作栏( *** 作栏sherlock),我可以通过标签切换并更改碎片. 每个片段都有一个webview,我试图保存webview的状态,以便网页停留在用户离开它的同一页面上. 这是我的片段代码: public class FragmentB extends SherlockFragment {WebView myWebView; 我一直试图在一周的最佳时间做这件事但仍然没有运气.

我有一个带有标签的 *** 作栏( *** 作栏sherlock),我可以通过标签切换并更改碎片.

每个片段都有一个webvIEw,我试图保存webvIEw的状态,以便网页停留在用户离开它的同一页面上.

这是我的片段代码:

public class FragmentB extends SherlockFragment {WebVIEw myWebVIEw;@OverrIDepublic voID onSaveInstanceState(Bundle outState) {   super.onSaveInstanceState(outState);   myWebVIEw.saveState(outState);   Log.w("///////","onSaveInstanceState");}@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) {   super.onActivityCreated(savedInstanceState);   myWebVIEw.restoreState(savedInstanceState);   Log.w("///////","onActivityCreated");}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    Log.w("///////","onCreate");}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {     VIEw mainVIEw = (VIEw) inflater.inflate(R.layout.frag_b,container,false);     myWebVIEw = (WebVIEw) mainVIEw.findVIEwByID(R.ID.webvIEw);     Log.w("///////","onCreateVIEw");     if (savedInstanceState != null){         myWebVIEw.restoreState(savedInstanceState);         Log.w("///////","restore!");     }     else{         Log.w("///////","ELSE!");         myWebVIEw.setWebVIEwClIEnt(new MyWebVIEwClIEnt());         myWebVIEw.getSettings().setPluginsEnabled(true);         myWebVIEw.getSettings().setBuiltInZoomControls(false);          myWebVIEw.getSettings().setSupportZoom(false);         myWebVIEw.getSettings().setJavaScriptCanopenwindowsautomatically(true);            myWebVIEw.getSettings().setAllowfileAccess(true);          myWebVIEw.getSettings().setDomStorageEnabled(true);         myWebVIEw.loadUrl("http://www.Google.com");        }    //return inflater.inflate(R.layout.frag_b,false);    return mainVIEw;}public class MyWebVIEwClIEnt extends WebVIEwClIEnt {            @OverrIDe    public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw,String url) {        if (url.endsWith(".mp4"))         {            Intent intent = new Intent(Intent.ACTION_VIEW);            intent.setDataAndType(Uri.parse(url),"vIDeo/*");            vIEw.getContext().startActivity(intent);            return true;        }         else {            return super.shouldOverrIDeUrlLoading(vIEw,url);        }    }}

}

任何帮助都会很棒!

解决方法 只有当父活动需要保存其状态时,才会调用片段的onSaveInstanceState方法;例如,在方向更改期间销毁/重新创建父活动时.因此,当您只是替换片段或在它们之间切换为制表符时,将不会调用此方法.

作为特定情况的解决方法,您可以在onPause方法中将webvIEw的状态保存在一个包中,并在onActivityCreated方法中将其恢复:

// Instance fIElds            private WebVIEw webVIEw;    private Bundle webVIEwBundle;    /**     * Save the state of the webvIEw     */    @OverrIDe    public voID onPause()    {        super.onPause();        webVIEwBundle = new Bundle();        webVIEw.saveState(webVIEwBundle);    }    /**     * Restore the state of the webvIEw     */    @OverrIDe    public voID onActivityCreated(Bundle savedInstanceState)    {        super.onActivityCreated(savedInstanceState);        if (webVIEwBundle != null)        {            webVIEw.restoreState(webVIEwBundle);        }    }

有关更多信息,请参见此处:

How to save the state of a WebView inside a Fragment of an Action Bar

总结

以上是内存溢出为你收集整理的android – 更改片段并保存Web视图状态 – 变得疯狂全部内容,希望文章能够帮你解决android – 更改片段并保存Web视图状态 – 变得疯狂所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1127190.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存