3

How my app can know when user logout from its native Facebook app ?

I am using below code for Logging out from Facebook in my app. And it is working fine.

class LogoutTask extends AsyncTask<Void, Void, Void> {

       @Override
       protected Void doInBackground(Void... params) {
            if (facebook.isSessionValid()) {
                try {
                    facebook.logout(getApplicationContext());
                    editLoginStatus(false);
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", null);
                    editor.putLong("access_expires",
                            facebook.getAccessExpires());
                    editor.commit();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }

    }

But problem is occurring when user logging out from its native Facebook app and then clicking on the logout button on my app.

It showing me following error

 java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:299)
 at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.IllegalArgumentException: Invalid context argument
 at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:86)
 at com.facebook.internal.Utility.clearCookiesForDomain(Utility.java:319)
 at com.facebook.internal.Utility.clearFacebookCookies(Utility.java:343)
 at com.facebook.Session.closeAndClearTokenInformation(Session.java:798)
 at com.facebook.android.Facebook.logoutImpl(Facebook.java:665)
 at com.facebook.android.Facebook.logout(Facebook.java:642)
 at com.example.animation.StartLoginActivity$LogoutTask.doInBackground(StartLoginActivity.java:328)
 at com.example.animation.StartLoginActivity$LogoutTask.doInBackground(StartLoginActivity.java:1)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

and I am calling this function as below

if (facebook.getAccessToken() != null && facebook.isSessionValid() == true) {
    new LogoutTask().execute();
}

here accessToken I am storing locally in sharedPreference , so it won't be null as long as I am not logging out from my app , but I guess after logging out from native FB app facebook.isSessionValid() should return false while it is returning true .So how to check to know Facebook app is already logging out or not ?

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • Someone said to me that after user logged out then previous session will be outdated. So now my question is how I can check that session is outdated or not ? – Kunu Aug 21 '14 at 07:31

3 Answers3

1

on destroy you clear the session and token information so that if you logout from your native application .. it will clear its credentials

use this

  @Override 
  protected void onDestroy() {

   // TODO Auto-generated method stub
  super.onDestroy();
 Session session = Session.getActiveSession();
 session.closeAndClearTokenInformation();


  }

OR

   public void logoutFromFacebook() {
   mAsyncRunner.logout(this, new RequestListener() {
    @Override
    public void onComplete(String response, Object state) {
        Log.d("Logout from Facebook", response);
        if (Boolean.parseBoolean(response) == true) {
            // User successfully Logged out
        }
    }

    @Override
    public void onIOException(IOException e, Object state) {
    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
    }
});
}

try this it works for me .. so it will also resolve your issue also

Vaishali Sutariya
  • 5,093
  • 30
  • 32
  • I am not using `Session.getActiveSession()` for storing my session data rather I am using `facebook.getSession`. So when I am writing `facebook.getSession.closeAndClearTokenInformation()` in OnDestroy it showing me `java.lang.IllegalArgumentException: Invalid context argument` error. – Kunu Aug 23 '14 at 04:54
  • see again answer i have updated .. or u can refer this url http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ – Vaishali Sutariya Aug 23 '14 at 05:01
  • Still having same error , It showing `Invalid context argument` while clicking on Logout button – Kunu Aug 23 '14 at 05:24
  • there this exception is about passing context problem.. so instead of passing " this " please pass this.getApplicationContext() Certainly, this is not the best solution, but it gets the job done. – Vaishali Sutariya Aug 23 '14 at 05:31
  • Still no luck, I tried that as well as tried only `getApplicationContext()` also – Kunu Aug 23 '14 at 05:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59842/discussion-between-kunu-and-vaishali). – Kunu Aug 23 '14 at 06:08
1

Try to check if there's a Facebook account in the system.

AccountManager accountManager = AccountManager.get(context);
Account[] facebookAccounts = accountManager.getAccountsByType("com.facebook.auth.login");
if (facebookAccounts.length > 0) {
    facebook.logout(getApplicationContext());
    ...
}

Yes, the code contains hard-coded account type, which is slightly unreliable due to possible changes in the native app, though the probability of such changes is very small.

And don't forget to add a GET_ACCOUNTS permission to the Manifest.

  • If user log out from Facebook native app then session will be outdated ,but if he again login from the facebook app session will be a different one however in this method facebookAccounts.length will return 1 – Kunu Aug 28 '14 at 06:50
0

Hmmmm... It may be get tricky. I guess the isSessionValid is offline checking from the FB API.

From your situation, it looks like you still need to have a single call to the FB to check the session is really valid or not. So my suggestion based on your situation is to make simple FB API call such as /me just to check the validity of the session.

If session is valid, then you manually call the logout API, otherwise, you know that the FB session is either expired/logged out.

Hope helps :)

kororo
  • 1,972
  • 15
  • 26