Last active
December 21, 2015 22:19
-
-
Save granoeste/6374688 to your computer and use it in GitHub Desktop.
[Android] Template that uses 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
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); | |
} | |
} |
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
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(); | |
} | |
} |
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
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