简体   繁体   中英

Android, check if user is logged with Facebook

I am struggling to find a clear way, how to determine, if user is logged in with Facebook on my android application.

Official Facebook documentation says that:

You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile().

However, AccessToken.getCurrentAccessToken() will be available only if SDK initialization was completed. So as far as I understand, such approach might not work all the time:

FacebookSdk.sdkInitialize(this.getApplicationContext());
if (AccessToken.getCurrentAccessToken() != null) {
  //good
}

Because, as it was discussed in this question:

How can i find my current login status - facebook API android

It says, that AccessTokenTracker() callback onCurrentAccessTokenChanged() might be fired async and therefore, it is possible to get null value of getCurrentAccessToken() in this case above (I would simply loose the race).

So if there is a chance of having null value of getCurrentAccessToken right after SDK init, and if, as far as I know, onCurrentAccessTokenChanged() is NEVER executed if user is not logged in at all, so... how to check that properly? This seems like a deadlock to me. Am I missing here something?

What I have done : I have looked at official FB manual, implemented login in general, but this issue is giving me headaches. Also check some other SO questions, but all solutions do not explain what I want.

Moreover, I want to check this on application startup, so I do not have such things like onResume , onStart etc.

What i'm doing to verify if a user is logged or not I do this: 1- On the method "OnCreate" I initialize the SDK, and the facebook callback manager. 2- Setup the contentview 3- Setup the Token and Profile trackers and then start them. 4- Setup the UI, getting the login button facebook event. 5- On the method "OnResume" I check this:

if(Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){
   //You are logged
}

Also you I check if the user is logged on the method of the setup tracking. I wish I had expressed myself well

try this:

public boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}

or check this: login fb

Yes, this approach would work normally, but I want to check, if user is logged in, when application is started, so no such things like onResume is available.

What I did to check that, I create a Splash activity, and in that activity check if the user is signed in (yes, I initialized the SDK in the splash activity), and depending on that result, I start login activity or the main activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM