Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created October 1, 2019 12:32
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 yunusemredilber/38798b5a5eeb4e50058e04a020afd4b6 to your computer and use it in GitHub Desktop.
Save yunusemredilber/38798b5a5eeb4e50058e04a020afd4b6 to your computer and use it in GitHub Desktop.
Turbolinks Android - Fix Session Issues
public class JsBridge {
private Context context;
public static android.webkit.CookieManager cookieManager = CookieManager.getInstance();
public JsBridge(Context context){
this.context = context;
}
@JavascriptInterface
public void clearSession() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
// A callback which is executed when the cookies have been removed
@Override
public void onReceiveValue(Boolean aBoolean) {
Log.d(TAG, "Cookie removed: " + aBoolean);
}
});
}
else cookieManager.removeAllCookie();
}
}
AndroidBridge.clearSession(); // Run this command from web when logging out.
public class MainActivity extends AppCompatActivity implements TurbolinksAdapter {
// ...
protected void onCreate(Bundle savedInstanceState) {
// ...
// Add JS Interface
TurbolinksSession.getDefault(this).getWebView().addJavascriptInterface(new JsBridge(this),"AndroidBridge");
// ...
}
@Override
public void visitCompleted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
JsBridge.cookieManager.flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment