SHOW:
|
|
- or go back to the newest paste.
| 1 | public class ImprovedWebViewFragment extends Fragment {
| |
| 2 | ||
| 3 | private WebView mWebView; | |
| 4 | private boolean mIsWebViewAvailable; | |
| 5 | private String mUrl = null; | |
| 6 | ||
| 7 | public ImprovedWebViewFragment() {
| |
| 8 | } | |
| 9 | - | public static ImprovedWebViewFragment loadFragment(String url) {
|
| 9 | + | |
| 10 | - | |
| 10 | + | public ImprovedWebViewFragment(String url) {
|
| 11 | - | ImprovedWebViewFragment swvf = new ImprovedWebViewFragment(); |
| 11 | + | super(); |
| 12 | - | Bundle b = new Bundle(); |
| 12 | + | mUrl = url; |
| 13 | - | b.putString("url", url);
|
| 13 | + | |
| 14 | - | swvf.setArguments(b); |
| 14 | + | |
| 15 | - | return swvf; |
| 15 | + | |
| 16 | - | |
| 16 | + | |
| 17 | */ | |
| 18 | @Override | |
| 19 | public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| 20 | Bundle savedInstanceState) {
| |
| 21 | if (mWebView != null) {
| |
| 22 | mWebView.destroy(); | |
| 23 | } | |
| 24 | mWebView = new WebView(getActivity()); | |
| 25 | mWebView.loadUrl(mUrl); | |
| 26 | mIsWebViewAvailable = true; | |
| 27 | return mWebView; | |
| 28 | } | |
| 29 | - | mWebView.loadUrl(getArguments() != null ? getArguments().getString("url") : null);
|
| 29 | + | |
| 30 | /** | |
| 31 | * Convenience method for loading a url. Will fail if {@link View} is not initialised (but won't throw an {@link Exception})
| |
| 32 | * @param url | |
| 33 | */ | |
| 34 | public void loadUrl(String url) {
| |
| 35 | if (mIsWebViewAvailable) getWebView().loadUrl(mUrl = url); | |
| 36 | else Log.w("ImprovedWebViewFragment", "WebView cannot be found. Check the view and fragment have been loaded.");
| |
| 37 | } | |
| 38 | ||
| 39 | - | if (mIsWebViewAvailable) getWebView().loadUrl(url); |
| 39 | + | |
| 40 | * Called when the fragment is visible to the user and actively running. Resumes the WebView. | |
| 41 | */ | |
| 42 | @Override | |
| 43 | public void onPause() {
| |
| 44 | super.onPause(); | |
| 45 | mWebView.onPause(); | |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Called when the fragment is no longer resumed. Pauses the WebView. | |
| 50 | */ | |
| 51 | @Override | |
| 52 | public void onResume() {
| |
| 53 | mWebView.onResume(); | |
| 54 | super.onResume(); | |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Called when the WebView has been detached from the fragment. | |
| 59 | * The WebView is no longer available after this time. | |
| 60 | */ | |
| 61 | @Override | |
| 62 | public void onDestroyView() {
| |
| 63 | mIsWebViewAvailable = false; | |
| 64 | super.onDestroyView(); | |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Called when the fragment is no longer in use. Destroys the internal state of the WebView. | |
| 69 | */ | |
| 70 | @Override | |
| 71 | public void onDestroy() {
| |
| 72 | if (mWebView != null) {
| |
| 73 | mWebView.destroy(); | |
| 74 | mWebView = null; | |
| 75 | } | |
| 76 | super.onDestroy(); | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Gets the WebView. | |
| 81 | */ | |
| 82 | public WebView getWebView() {
| |
| 83 | return mIsWebViewAvailable ? mWebView : null; | |
| 84 | } | |
| 85 | ||
| 86 | } |