Here is my code:
public class LocationDetailActivity extends ActionBarActivity {
private CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack;
private ImageView mBtnBack;
AccessToken token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_detail_layout);
FacebookSdk.sdkInitialize(getApplicationContext());
share = (ImageButton) findViewById(R.id.imageShare);
token = AccessToken.getCurrentAccessToken();
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(token == null) {
final Dialog dialog = new Dialog(LocationDetailActivity.this);
dialog.setContentView(R.layout.share_custom_dialog);
dialog.setTitle("Login as:");
dialog.show();
mCallBackManager = CallbackManager.Factory.create();
mCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if(profile!=null){
dialog.cancel();
ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
if(shareDialog.canShow(ShareLinkContent.class)){
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
shareDialog.show(content);
}
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
LoginButton loginButton = (LoginButton)dialog.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(mCallBackManager, mCallBack);
}
else if(token != null){
ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
if(shareDialog.canShow(ShareLinkContent.class)){
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
shareDialog.show(content);
}
}
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallBackManager.onActivityResult(requestCode, resultCode, data);
}
}
I want to check if user already logged in from Facebook's app. If they didn't, my app will show a Custom Dialog with login button. Otherwise, my app will show Share Dialog, but when I click on my button, the Custom Dialog always be showed even I logged-in Facebook.
More info: AccessToken token = AccessToken.getCurrentAccessToken();
Check Login Status
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
Then you can later perform the actual login, such as in a custom button's OnClickListener:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With