Instead of using a normal button for login/logout purposes, you can use Facebook SDK's own Login/Logout Widget. You can find the implementation details in the following link:
Post to facebook after login fails Android
EDIT:
After initializing the mFacebookButton
, set these permissions:
mFacebookButton.setPublishPermissions(Arrays.asList("publish_stream","read_stream"));
Use the code below after setting the permissions correctly.
mFacebookButton.setSessionStatusCallback(new Session.StatusCallback()
{
public void call(Session session, SessionState state,Exception exception)
{
// TODO Auto-generated method stub
if (session.isOpened())
{
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
public void onCompleted(GraphUser user,Response response)
{
// TODO Auto-generated method stub
if (user != null)
{
Log.i(TAG, "User ID " + user.getId());
Log.i(TAG,"Email "+ user.asMap().get("email"));
}
}});
}
else
{
//This else condition will be executed when logout will be clicked.
}
}
});
Instead of this code:
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
});
I hope this helps.