简体   繁体   English

如果未安装应用程序,则Facebook登录无法启动第二个活动

[英]Facebook login doesn't start second activity if app is not installed

I'm trying to implement the Facebook Login on an Android app. 我正在尝试在Android应用程序上实现Facebook登录。 I followed the Facebook Developer guide and it's working but if Facebook app is not installed then it takes login details,logs in and stays on same activity and just show logout button, if I close application and start again then it works perfectly fine. 我遵循了Facebook开发人员指南,并且可以正常工作,但是如果未安装Facebook应用程序,则它将使用登录详细信息,登录并保持相同的活动并仅显示注销按钮,如果我关闭应用程序并再次启动,则可以正常运行。

public class MainActivity extends AppCompatActivity {

    LoginButton loginButton;
    CallbackManager callbackManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        isLoggedIn();

        loginButton = (LoginButton) findViewById(R.id.login_button);

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken   accessToken = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();
                nextActivity(profile);

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });

        loginButton.setReadPermissions("user_friends");



    }

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        //Facebook login
        callbackManager.onActivityResult(requestCode, responseCode, intent);

    }

    public void isLoggedIn() {

        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken!=null) {

            Profile profile=Profile.getCurrentProfile();

            nextActivity(profile);
        }
    }

    private void nextActivity(Profile profile) {
        if (profile != null) {                                                                                        Intent main = new Intent(MainActivity.this, Result.class);
            main.putExtra("Name", profile.getFirstName());
            main.putExtra("Surname", profile.getLastName());
            startActivity(main);
            finish();
        }
    }
}

Instead of using LoginManager.getInstance().registerCallback() use loginButton.registerCallback() 代替使用LoginManager.getInstance().registerCallback()使用loginButton.registerCallback()

EDIT: 编辑:

Inside onSuccess have following code: 在onSuccess内部有以下代码:

    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        if(Profile.getCurrentProfile() == null) {
                ProfileTracker profileTracker = new ProfileTracker() {
                        @Override
                        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                               this.stopTracking();
                               nextActivity(currentProfile);
                        }
                };
                profileTracker.startTracking();
        }else{
               Profile profile = Profile.getCurrentProfile();
               nextActivity(profile);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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