5

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();

karel
  • 5,489
  • 46
  • 45
  • 50
naji kun
  • 167
  • 3
  • 10
  • when you create your token? – PedroHawk May 05 '15 at 11:06
  • I create token in onCreate in MainActivity.This is just to test so the MainActivity is very simple,it just contains only 1 button. – naji kun May 06 '15 at 03:00
  • did u check if your token refreshs after any login/logout action? do you implement some callback action? – PedroHawk May 06 '15 at 11:06
  • I think I did. I updated with the whole activity. This is the first time I use Facebook SDK, please help me to check it ;) – naji kun May 06 '15 at 16:23
  • just looking at your code, when you click "share" he verifies if token is null... if it is null (looks like its allways) you request a login dialog. try to put inside of **onSuccess()** a new value for token (getCurrentAccessToken()) and there you must get new value for your new session. – PedroHawk May 07 '15 at 13:49
  • please, see my answer. hope it helps – PedroHawk May 07 '15 at 13:57
  • I think I knew what the problem is. I must first login to Facebook through **Facebook's LogginButton** in my app to get AccessToken or else it always null, right? So I think AccessTokenTracker can't fix that and what I want to know is how can I get AccessToken from **FaceBook App** to declare in my app. – naji kun May 08 '15 at 05:27
  • remember you can already have an active session in memory to avoid repeat login... please see answer below, see if it helps. – PedroHawk May 08 '15 at 08:45
  • Possible duplicate of [How to check if user is logged in with FB SDK 4.0 for Android?](http://stackoverflow.com/questions/29294015/how-to-check-if-user-is-logged-in-with-fb-sdk-4-0-for-android) – Debosmit Ray Apr 15 '16 at 08:03

2 Answers2

1

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"));

Vikram vora
  • 51
  • 1
  • 3
0

on a fast research, you must implement new methods to verify an active session on facebook SDK 4.0, like:

  • initialized your Facebok SDK (i guess you already do it)
  • Implement AccessTokenTracker

try to see this post, i guess it can help you

Community
  • 1
  • 1
PedroHawk
  • 622
  • 5
  • 19