How to logout from twitter using Fabric Sdk for android
Asked Answered
S

6

6

I used

Twitter.getSessionManager().clearActiveSession();

This does not work,next time when i logIn using twitter, it opens the dialog with browser,takes previous login and just asks "Allow app to fetch your data?", but doesn't ask for username and password.Any help will be appreciated.

Saadi answered 20/4, 2015 at 9:13 Comment(1)
tried deleting Application cache directory and also deleted browser data using Browser.clearHistory(getContentResolver()); , still the same issueSaadi
S
35

I finally found a solution to this situation.

Accidentally, I found a method in Twitter SDK Kit for Android

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
Twitter.getSessionManager().clearActiveSession();
Twitter.logOut();

This was very simple, it took me about half an hour to find it.

For version 3.0 and above

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
TwitterCore.getInstance().getSessionManager().clearActiveSession()
Saadi answered 20/4, 2015 at 9:37 Comment(10)
know how to do this for IOS objective c?Leges
I literally cannot find any reference to this anywhere. I wonder if the method is asynchronous, or what it actually does. Fabric is poorly documented :(Redintegrate
The CookieSyncManager is now deprecated. CookieManager.flush is a possible alternative.Promontory
'android.webkit.CookieSyncManager' is deprecated. @Promontory can you please show sample snippet for how to use CookieManager.flush?Vonnie
@DroidWormNarendra if you still need help with that, you may check my answer below.Hilten
the Twitter.logOut() method is no longer available , the ide says 'can not resolve method logOut()' any alternatives?Brainpan
@AhmedRajab I have same problem. Did you deal with it ?Irksome
@user7856586 when i log in or out twitter i user a shared preference which holds a boolean value called "twitter" to identify whether i am logged in or out and change the UI according to that....... after i log out I clear cookies as described in the answers to stop remembring my accountBrainpan
@AhmedRajab thnxIrksome
can u please upvote my comment if u find it useful @user7856586Brainpan
H
13

Now that CookieSyncManager is deprecated, I do it this way:

public void logoutTwitter() {
        TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
        if (twitterSession != null) {
            ClearCookies(getApplicationContext());
            Twitter.getSessionManager().clearActiveSession();
            Twitter.logOut();
        }
}

public static void ClearCookies(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
            CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager=CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
Hilten answered 11/2, 2016 at 21:23 Comment(0)
L
4

LAST WORKING solution:

public static void logoutTwitter() {
    TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();
    if (twitterSession != null) {
        clearTwitterCookies(mAppContext);
        Twitter.getSessionManager().clearActiveSession();
        Twitter.logOut();
    }
}

public static void clearTwitterCookies(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();

    } else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}
Liking answered 2/11, 2016 at 19:5 Comment(1)
I'm sorry but this doesn't work for me. I always get the last user logged. I'm using the last version.Hands
P
2

The above methods are extinct.

// twitter log out
TwitterCore.getInstance().getSessionManager().clearActiveSession();
Phospholipide answered 16/8, 2017 at 15:15 Comment(0)
S
1

You can now use:

Digits.logout();
Sapsucker answered 29/3, 2017 at 18:41 Comment(0)
M
0

To logout current user simply call

mTwitter.shutdown();
Maggiemaggio answered 18/4, 2018 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.