Skip to content

Instantly share code, notes, and snippets.

@granoeste
Last active December 21, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save granoeste/6374688 to your computer and use it in GitHub Desktop.
Save granoeste/6374688 to your computer and use it in GitHub Desktop.
[Android] Template that uses WebView
public class MyApplication extends Application {
@Override
public void onCreate() {
// To use the CookieSyncManager, the host application has to call the following when the application starts:
CookieSyncManager.createInstance(this);
}
}
public class WebViewActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
// forces sync manager to sync now
CookieSyncManager.getInstance().sync();
}
);
if (savedInstanceState != null) {
// Restores the state of this WebView
mWebView.restoreState(savedInstanceState);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// Saves the state of this WebView
mWebView.saveState(outState);
}
@Override
protected void onResume() {
// Resumes all layout, parsing, and JavaScript timers for all WebViews.
mWebView.resumeTimers();
// Requests sync manager to start sync
CookieSyncManager.getInstance().startSync();
}
@Override
protected void onPause() {
// Pauses all layout, parsing, and JavaScript timers for all WebViews.
mWebView.pauseTimers();
// Requests sync manager to stop sync.
CookieSyncManager.getInstance().stopSync();
}
}
public class MainFragment extends Fragment {
private WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
FrameLayout root = new FrameLayout(getActivity());
mWebView = new WebView(getActivity());
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
public void onPageFinished(WebView view, String url) {
// forces sync manager to sync now
CookieSyncManager.getInstance().sync();
}
});
root.addView(mWebView, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return root;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null) {
// Restores the state of this WebView
mWebView.restoreState(savedInstanceState);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Saves the state of this WebView
mWebView.saveState(outState);
}
@Override
public void onResume() {
super.onResume();
// Resumes all layout, parsing, and JavaScript timers for all WebViews.
mWebView.resumeTimers();
// Requests sync manager to start sync
CookieSyncManager.getInstance().startSync();
}
@Override
public void onPause() {
super.onPause();
// Pauses all layout, parsing, and JavaScript timers for all WebViews.
mWebView.pauseTimers();
// Requests sync manager to stop sync.
CookieSyncManager.getInstance().stopSync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment