简体   繁体   中英

best way skip facebook login page in android if it is already logged in

I am login my android app using the Facebook login. Here i am checking the Access Token is null or not so that i can move to another activity after login. This is working fine. please help is there any better way to move another activity when the facebook is already logged in?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_boringmain);

    callbackManager = CallbackManager.Factory.create();
    List<String> permissions = new ArrayList<>();
    permissions.add("email");

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

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
            Profile.setCurrentProfile(profile1);
        }
    };

    profileTracker.startTracking();

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,AccessToken currentAccessToken) {
                 AccessToken.setCurrentAccessToken(currentAccessToken);
        }
    };

     if (AccessToken.getCurrentAccessToken()!=null) {
        Intent intent = new Intent(boringmain.this, UserProfile.class);
        startActivity(intent);
      } 
     else 
      { 
        Log.e("S","User not logged in "); 
       }

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            getUserDetails(loginResult);

        };

        @Override
        public void onCancel() {
            // App code
        }

is there any better method do the above scenario? The usage of profile tracker and access token tracker is correct?

Checking if user is logged in by calling

if (AccessToken.getCurrentAccessToken()!=null) 

is good way. But in my opinion better place to check it is before opening Login Activity. You can call this code everywhere you want and if AccessToken.getCurrentAccessToken() is null then open Login Activity

in Login Activity you should go to another activity after success login here:

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
       //START NEW ACTIVITY AFTER CORRECT LOGIN HERE

      Intent intent = new Intent(boringmain.this, UserProfile.class);
      startActivity(intent);
    };

    @Override
    public void onCancel() {
    };
  });

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