Hi, I am trying to use a WebViewFragment in order to retain an
instance of WebView even when the Fragment is not displayed.

My goal is to have the names of several websites in the ActionBar.
When users tap on one of the names, the corresponding website is
opened in a WebViewFragment. When users open a website that has been
opened before, the old WebView should get restored.

Right now, I have a subclass of WebViewFragment that looks like this:

public class MyWebViewFragment extends WebViewFragment {
    private String mURL;

    public static MyWebViewFragment getInstance(String url) {
        MyWebViewFragment f = new MyWebViewFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mURL = getArguments().getString("url");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        WebView webView = getWebView();
        if (webView != null) {
            if (webView.getOriginalUrl() == null) {
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadUrl(mURL);
            }
        }
    }
}

When the users press a button in the ActionBar, I execute the
following method:

    private void addWindow(String url) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        MyWebViewFragment currentWindow = (MyWebViewFragment)
fm.findFragmentById(R.id.fragment_container);

        if (currentWindow != null) {
            ft.remove(currentWindow);
        }

        //See if the window we are requesting has already been created
        MyWebViewFragment newWindow = (MyWebViewFragment)
fm.findFragmentByTag(url);

        if (newWindow == null) {
            newWindow = MyWebViewFragment.getInstance(url);
        }

        ft.add(R.id.fragment_container, newWindow, url);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }

However, the state of the WebView is not retained when I recover a
Fragment using the FragmentManager. It seems that onCreateView returns
a fresh WebView every time the View is created.
How can I avoid this behavior? Is it even possible to retain an
instance of WebViewFragment over time? I mean, it should be possible
because the Android browser needs to do it as well.

Any advice would be very much appreciated!

Cheers,

Philipp

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to