Last active
September 14, 2015 02:35
-
-
Save juanmendez/81a1cd6831634775fe07 to your computer and use it in GitHub Desktop.
Demo for WebViewFragment supporting android.support.v4.app.Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.juanmendez.client; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
public class ClientWebViewFragment extends WebViewFragment | |
{ | |
public static ClientWebViewFragment build( String url ) | |
{ | |
ClientWebViewFragment fragment = new ClientWebViewFragment(); | |
Bundle bundle = new Bundle(); | |
bundle.putString("url", url); | |
fragment.setArguments(bundle); | |
return fragment; | |
} | |
public String getUrl(){ | |
return getArguments().getString("url"); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
WebView webView = (WebView) super.onCreateView(inflater, container, savedInstanceState ); | |
if( !rotated() ) | |
{ | |
webView.getSettings().setJavaScriptEnabled(true); | |
webView.setWebViewClient(new WebViewClient() { | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
return false; | |
} | |
}); | |
webView.loadUrl(getUrl()); | |
} | |
return webView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.juanmendez.support; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.webkit.WebView; | |
/** | |
* WebViewFragment support for android.support.v4.app.Fragment | |
* This demo has been tested for SDK 10 and 22. | |
* | |
* @Author @juanmendezinfo | |
* @see <a href="http://www.juanmendez.info/2015/09/webviewfragment-which-supports.html">details</a> | |
*/ | |
public class WebViewFragment extends Fragment | |
{ | |
private WebView mWebView; | |
private boolean mIsWebViewAvailable; | |
private boolean mRotated = false; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
setRetainInstance(true); | |
if( mWebView == null ) | |
{ | |
mWebView = new WebView(getActivity()); | |
} | |
return mWebView; | |
} | |
/** | |
* let us know if the webView has been rotated. | |
* @return | |
*/ | |
public boolean rotated() { | |
return mRotated; | |
} | |
/** | |
* Called when the fragment is visible to the user and actively running. Resumes the WebView. | |
*/ | |
@Override | |
public void onPause() { | |
super.onPause(); | |
if (honeyOrHigher()) | |
mWebView.onPause(); | |
mRotated = true; | |
} | |
/** | |
* Called when the fragment is no longer resumed. Pauses the WebView. | |
*/ | |
@Override | |
public void onResume() { | |
if (honeyOrHigher()) | |
mWebView.onResume(); | |
super.onResume(); | |
} | |
private boolean honeyOrHigher(){ | |
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; | |
} | |
/** | |
* Called when the WebView has been detached from the fragment. | |
* The WebView is no longer available after this time. | |
*/ | |
@Override | |
public void onDestroyView() { | |
mIsWebViewAvailable = false; | |
if( mWebView != null ) | |
{ | |
ViewGroup parentViewGroup = (ViewGroup) mWebView.getParent(); | |
if (parentViewGroup != null) { | |
parentViewGroup.removeView(mWebView); | |
} | |
} | |
super.onDestroyView(); | |
} | |
/** | |
* Called when the fragment is no longer in use. Destroys the internal state of the WebView. | |
*/ | |
@Override | |
public void onDestroy() { | |
if (mWebView != null) { | |
mWebView.destroy(); | |
mWebView = null; | |
} | |
super.onDestroy(); | |
} | |
/** | |
* Gets the WebView. | |
*/ | |
public WebView getWebView() { | |
return mIsWebViewAvailable ? mWebView : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment