1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
19import android.app.Fragment;
20import android.os.Bundle;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.webkit.WebView;
25
26/**
27 * A fragment that displays a WebView.
28 * <p>
29 * The WebView is automically paused or resumed when the Fragment is paused or resumed.
30 */
31public class WebViewFragment extends Fragment {
32    private WebView mWebView;
33    private boolean mIsWebViewAvailable;
34
35    public WebViewFragment() {
36    }
37
38    /**
39     * Called to instantiate the view. Creates and returns the WebView.
40     */
41    @Override
42    public View onCreateView(LayoutInflater inflater, ViewGroup container,
43            Bundle savedInstanceState) {
44        if (mWebView != null) {
45            mWebView.destroy();
46        }
47        mWebView = new WebView(getActivity());
48        mIsWebViewAvailable = true;
49        return mWebView;
50    }
51
52    /**
53     * Called when the fragment is visible to the user and actively running. Resumes the WebView.
54     */
55    @Override
56    public void onPause() {
57        super.onPause();
58        mWebView.onPause();
59    }
60
61    /**
62     * Called when the fragment is no longer resumed. Pauses the WebView.
63     */
64    @Override
65    public void onResume() {
66        mWebView.onResume();
67        super.onResume();
68    }
69
70    /**
71     * Called when the WebView has been detached from the fragment.
72     * The WebView is no longer available after this time.
73     */
74    @Override
75    public void onDestroyView() {
76        mIsWebViewAvailable = false;
77        super.onDestroyView();
78    }
79
80    /**
81     * Called when the fragment is no longer in use. Destroys the internal state of the WebView.
82     */
83    @Override
84    public void onDestroy() {
85        if (mWebView != null) {
86            mWebView.destroy();
87            mWebView = null;
88        }
89        super.onDestroy();
90    }
91
92    /**
93     * Gets the WebView.
94     */
95    public WebView getWebView() {
96        return mIsWebViewAvailable ? mWebView : null;
97    }
98}
99