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.

Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • tried deleting Application cache directory and also deleted browser data using `Browser.clearHistory(getContentResolver());` , still the same issue – Neal Ahluvalia Apr 20 '15 at 09:24

7 Answers7

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()
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
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();
        }
    }
Mohit Singh
  • 1,452
  • 1
  • 18
  • 27
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();
    }
}
Choletski
  • 7,074
  • 6
  • 43
  • 64
2

The above methods are extinct.

// twitter log out
TwitterCore.getInstance().getSessionManager().clearActiveSession();
natedogg265
  • 239
  • 2
  • 6
1

You can now use:

Digits.logout();
Malek Hijazi
  • 4,112
  • 1
  • 26
  • 31
1
TwitterCore.getInstance().getSessionManager().clearActiveSession();

This one is the logout method when you are login with Twitter Auth

0

To logout current user simply call

mTwitter.shutdown();
Sahil Hamirani
  • 141
  • 1
  • 5