blob: e5b7c8d23a090f0e5bd76f365eb74feae7c9c0a4 [file] [log] [blame]
Steve Block1916e7f2010-12-16 17:40:05 +00001/*
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.
Ian Lake0a1feb82017-11-13 10:26:46 -080030 *
31 * @deprecated Manually call {@link WebView#onPause()} and {@link WebView#onResume()}
Steve Block1916e7f2010-12-16 17:40:05 +000032 */
Ian Lake0a1feb82017-11-13 10:26:46 -080033@Deprecated
Steve Block1916e7f2010-12-16 17:40:05 +000034public class WebViewFragment extends Fragment {
35 private WebView mWebView;
Steve Block071970d2011-06-30 15:02:15 +010036 private boolean mIsWebViewAvailable;
Steve Block1916e7f2010-12-16 17:40:05 +000037
38 public WebViewFragment() {
39 }
40
41 /**
42 * Called to instantiate the view. Creates and returns the WebView.
43 */
44 @Override
45 public View onCreateView(LayoutInflater inflater, ViewGroup container,
46 Bundle savedInstanceState) {
Steve Block071970d2011-06-30 15:02:15 +010047 if (mWebView != null) {
48 mWebView.destroy();
49 }
Adam Powell31479e32016-04-22 11:27:31 -070050 mWebView = new WebView(getContext());
Steve Block071970d2011-06-30 15:02:15 +010051 mIsWebViewAvailable = true;
Steve Block1916e7f2010-12-16 17:40:05 +000052 return mWebView;
53 }
54
55 /**
56 * Called when the fragment is visible to the user and actively running. Resumes the WebView.
57 */
58 @Override
59 public void onPause() {
60 super.onPause();
61 mWebView.onPause();
62 }
63
64 /**
65 * Called when the fragment is no longer resumed. Pauses the WebView.
66 */
67 @Override
68 public void onResume() {
69 mWebView.onResume();
70 super.onResume();
71 }
72
73 /**
Steve Block071970d2011-06-30 15:02:15 +010074 * Called when the WebView has been detached from the fragment.
75 * The WebView is no longer available after this time.
Steve Block1916e7f2010-12-16 17:40:05 +000076 */
77 @Override
78 public void onDestroyView() {
Steve Block071970d2011-06-30 15:02:15 +010079 mIsWebViewAvailable = false;
Steve Block1916e7f2010-12-16 17:40:05 +000080 super.onDestroyView();
81 }
82
83 /**
Steve Block071970d2011-06-30 15:02:15 +010084 * Called when the fragment is no longer in use. Destroys the internal state of the WebView.
85 */
86 @Override
87 public void onDestroy() {
88 if (mWebView != null) {
89 mWebView.destroy();
90 mWebView = null;
91 }
92 super.onDestroy();
93 }
94
95 /**
Steve Block1916e7f2010-12-16 17:40:05 +000096 * Gets the WebView.
97 */
98 public WebView getWebView() {
Steve Block071970d2011-06-30 15:02:15 +010099 return mIsWebViewAvailable ? mWebView : null;
Steve Block1916e7f2010-12-16 17:40:05 +0000100 }
101}