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.
3
Answers
try this:
or check this: login fb
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:
Also you I check if the user is logged on the method of the setup tracking.
I wish I had expressed myself well
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.