简体   繁体   中英

Validate already logged in to Facebook and open activity

I'm having an issue where I have a Facebook login button in the first activity a user sees when opening the app. The problem is, if they're already logged in to Facebook, I get a message that I am already logged in, but I only have the option to log out, and not to continue with the app. So basically I have to log out every time and log back in to actually use the app. So it says that I'm logged in and everything is fine, but I can't continue to the next activity of my app. I'm not sure if this is intended and that I didn't implement something or if this is an issue. Here is my code:

My onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    setContentView(R.layout.main_login);
    callbackManager = CallbackManager.Factory.create();

The setup for the login button:

loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_friends"));
}
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Intent myIntent = new Intent(getApplicationContext(), myActivity.class);

                    accessToken = AccessToken.getCurrentAccessToken();

                    GraphRequest request = GraphRequest.newMeRequest(
                            accessToken,
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {
                                    Log.v("LoginActivity", response.toString());
                                }
                            }

                    );
                    Bundle loginBundle = new Bundle();
                    loginBundle.putString("userInfo", "id, first_name, last_name, email");
                    request.setParameters(loginBundle);
                    request.executeAsync();

                    myIntent.putExtras(loginBundle);

                    startActivity(myIntent);

                    finish();
                }

                @Override
                public void onCancel() {
                    // nothing yet
                }

                @Override
                public void onError(FacebookException exception) {
                    // nothing yet
                }
            });

onActivityResult:

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
}

you can check if user is logged in and go directly to your activity

  if (AccessToken.getCurrentAccessToken()!=null){
        Intent myIntent = new Intent(getApplicationContext(), myActivity.class);
        startActivity(myIntent);
    }

Try like this, have a boolean flag in shared preference once the user finished logging in set the flag to true, while opening of the application check that flag open the log in activity only boolean flag is false,else open the main activity.
and give user an option to logout somewhere like menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM