Bryan Karlsson
Lurker
I have the following class that I instantiate from my fragment:
And here Is my fragment-class:
As you can see, Im trying to get the users profile when you click on the button. The login works, but the problem I have Is that when I click Login, else-statement Is executed and prints out NULL...... However, when I click Logout, I get the correct information, and the profile-object Is no longer NULL.
Why Is It null the first time I click on the button (login), and then becomes true when I click on It again (logout)?
When I place the profile-code inside onResume, It works. Why?
Java:
public class FacebookLogin {
private CallbackManager mCallbackManager;
private LoginButton lButton;
private AccessToken accessToken;
private Profile profile;
private static final String TAG = "FacbookLogin";
public FacebookLogin(Context c) {
FacebookSdk.sdkInitialize(c);
mCallbackManager = CallbackManager.Factory.create();
Log.i(TAG, " Constructor called");
}
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " logged in...");
AccessToken accessToken = loginResult.getAccessToken();
profile = Profile.getCurrentProfile(); //Access the profile who Is the person login i
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
Log.i(TAG, " Error");
Log.e(TAG, e.getMessage());
}
};
public void setCallback(LoginButton lButton) {
lButton = lButton;
lButton.registerCallback(mCallbackManager, mCallback);
Log.i(TAG, " setCallback called");
}
public CallbackManager getCallbackManager(){
return mCallbackManager;
}
public Profile getProfile() {
return profile;
}
}
And here Is my fragment-class:
Java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fLogin = new FacebookLogin(getActivity().getApplicationContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.login, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
final LoginButton loginButton = (LoginButton) getView().findViewById(R.id.login_button);
final TextView infoText = (TextView) getView().findViewById(R.id.text_details);
loginButton.setFragment(this);
loginButton.setReadPermissions("user_friends");
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, " Button clickde");
fLogin.setCallback(loginButton); //Let's register a callback
profile = fLogin.getProfile();
if (profile != null) {
Log.i(TAG, profile.getName());
} else {
Log.i(TAG, "NULL....... inside onresume");
}
}
});
}
As you can see, Im trying to get the users profile when you click on the button. The login works, but the problem I have Is that when I click Login, else-statement Is executed and prints out NULL...... However, when I click Logout, I get the correct information, and the profile-object Is no longer NULL.
Why Is It null the first time I click on the button (login), and then becomes true when I click on It again (logout)?
When I place the profile-code inside onResume, It works. Why?
Java:
@Override
public void onResume()
{
super.onResume();
profile = fLogin.getProfile();
if (profile != null) {
Log.i(TAG, profile.getName()); //Prints out directly when I hit the button
} else {
Log.i(TAG, "NULL....... inside onresume");
}
}
