保存碎片的状态? [英] Saving the state of fragments?

查看:108
本文介绍了保存碎片的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段类。下面是它如下:

I have a fragment class. Here is it below:

public class FragmentA extends Fragment {

Button button;
WebView myWebView;
int mCurCheckPosition;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

 }

 @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
     View mainView = (View) inflater.inflate(R.layout.frag_a, group, false);
     myWebView = (WebView) mainView.findViewById(R.id.webview);
    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.bbc.co.uk");       
    return mainView;
}


public class MyWebViewClient extends WebViewClient {        
    /* (non-Java doc)
     * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
     */


    @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);
        }
    }

}

}

现在的问题是,当我移动到和来自另一个片段,原来片段的状态(这是什么网页)都将丢失。

The problem is when I move to and from another fragment, the state of the original fragment (what web page it was on) is lost.

我怎么能prevent呢?我想要的网页的状态保持切换到,并且从每个片段。

How can I prevent this? I want the state of the web page to remain switching to and from each fragment.

感谢

推荐答案

您应该使用<一个href="http://developer.android.com/reference/android/webkit/WebView.html#saveState%28android.os.Bundle%29">WebView.saveState(Bundle状态),使用方法在你的onSaveInstanceState(包outState)方法,然后恢复状态<一href="http://developer.android.com/reference/android/webkit/WebView.html#restoreState%28android.os.Bundle%29">WebView.restoreState(Bundle状态)在onActivityCreated(包savedInstanceState)方法

You should use the WebView.saveState(Bundle state) method to in your onSaveInstanceState(Bundle outState) method and then restore the state using WebView.restoreState(Bundle state) in your onActivityCreated(Bundle savedInstanceState) method

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   mWebView.saveState(outState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);
   mWebView.restoreState(savedInstanceState);
}

此外,还要注意该片段生命周期。如果您不能确定在哪里恢复状态(的onCreate,onCreateView,onActivityCreated 的),看看该片段生命周期的文档找出正确的地方。 <一href="http://developer.android.com/guide/components/fragments.html">http://developer.android.com/guide/components/fragments.html

Also keep in mind the Fragment Lifecycle. If your unsure where to restore the state (onCreate, onCreateView, onActivityCreated), take a look at the Fragment Lifecycle documentation to figure out the right place. http://developer.android.com/guide/components/fragments.html

<一个href="http://developer.android.com/reference/android/app/Fragment.html#onCreate%28android.os.Bundle%29">onCreate()

在系统中创建的片段时,调用此。在您的实现,你应该初始化要保留在片段暂停或停止片段的重要组成部分,然后重新开始。

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

<一个href="http://developer.android.com/reference/android/app/Fragment.html#onCreateView%28android.view.LayoutInflater,%20android.view.ViewGroup,%20android.os.Bundle%29">onCreateView()

该系统调用这个时,它的时间段来绘制其用户界面的第一次。要绘制您片段的UI,你必须从这种方法是你的片段布局的根返回查看。你可以返回null如果该片段不提供用户界面。

The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

<一个href="http://developer.android.com/reference/android/app/Fragment.html#onActivityCreated%28android.os.Bundle%29">onActivityCreated()

片段的活动已创建这个片段的视图层次实例化时调用。它可以用来做最后的初始化,一旦这些作品都很到位,如检索意见或恢复状态。这也是使用setRetainInstance(布尔),以保持它们的实例片段很有用,因为这个回调告诉当它完全符合新的活动实例相关联的片段。这之后onCreateView(LayoutInflater,ViewGroup中,包)和ONSTART()之前调用。

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onStart().

这篇关于保存碎片的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆