kennedy484 kennedy484 - 6 months ago 87
Android Question

Facebook SDK(4.x): AccessToken.getCurrentAccessToken() returns null during run and not null in debug mode

Method One:In this case I set some place holder objects to the current accessToken and Profile

FacebookSdk.sdkInitialize(getApplicationContext());
// Initialize the SDK before executing any other operations,
// especially, if you're using Facebook UI elements.

AccessToken accessToken = AccessToken.getCurrentAccessToken();
Profile profile = Profile.getCurrentProfile();

//Attempts to recognize if the user has logged in before
if(accessToken !=null){
if(profile !=null) {
//Set up user parceable

//Start the Intent for the next Activity
//In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
//For now its just navigating to nearby
Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
startActivity(intent);
finish();
}
else{

}
}


Method Two: I simply use an if statement to check if getCurrentAccessToken() == null and if it does i set a boolean isLogged in to false. If the getCurrentAccessToken() == null condition returns I set isLoggedIn to true and then perform another if statement on isLoggedIn.

The problem is that inside of debug mode the code works perfectly, but when I just run it, no go, the app stays my login activity and requires the user to perform the login. BUT it doesn't prompt for an actual login it just overlays a spinning circle and then continues. So clearly the profile and accesstoken are there.

enter image description here

Answer

Instead of trying to retrieve the profile again, after I successfully login to facebook I save all the info I want, including the FB Id, to a SQLite DB. No issues anymore.

Old:

As of right now this code is catching the isLoggedIn 95% of the time. NOT 100% tho. I think it is just a timing issue, I wish there was a way to make sure that the isLoggedIn() doesn't get called until all the facbook resources are ready.

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //set the view to the saved data and change the layout
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    FacebookSdk.sdkInitialize(getApplicationContext());
    // Initialize the SDK before executing any other operations,
    // especially, if you're using Facebook UI elements.



    /*~~~~~~    Google API Setup                           */
    mGoogleApiClient = new GoogleApiClient.Builder(Splash.this)
            .addConnectionCallbacks(Splash.this)
            .addOnConnectionFailedListener(Splash.this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .build();

    /*~~~~~~~~~~~~~~    Facebook SDK setup and login event handler            */






    callbackManager = CallbackManager.Factory.create();








    //needs to be set in on activity result
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
                    startActivity(intent);
                }

                @Override
                public void onCancel() {
                    // App code
                    Log.d("FBLOGIN", "Cancel");
                    Toast.makeText(Splash.this, "Facebook Login Canceled", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Log.d("FBLOGIN", "Error");

                }
            });






    //initializes the Facebook SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar fb_seekbar = (SeekBar)findViewById(R.id.seekbar_fb);
    fb_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            fb_progress = progress;


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (fb_progress <= 75) {
                fb_seekbar.setProgress(4);
            } else {
                fb_seekbar.setProgress(100);
                //This is where the facebook sign in will be performed
                LoginManager.getInstance().logInWithReadPermissions(Splash.this, Arrays.asList("public_profile", "user_friends"));


                //Start the Intent for the next Activity
                //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
                //For now its just navigating to nearby
                //Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
                //startActivity(intent);
            }
        }
    });

    //initializes the Google Plus SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar google_slider = (SeekBar)findViewById(R.id.seekbar_google);
    google_slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            google_progress = progress;

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (google_progress <= 75) {
                google_slider.setProgress(0);
            } else {
                google_slider.setProgress(100);

                mGoogleApiClient.connect();

            }
        }
    });

    //initializes the Sign Up SeekBar Event Handlers
    //May be adding noises in here at a later date
    final SeekBar sign_up_seekbar = (SeekBar)findViewById(R.id.seekbar_regular);
    sign_up_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sign_up_progress = progress;

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if (sign_up_progress <= 75) {
                sign_up_seekbar.setProgress(4);
            } else {
                sign_up_seekbar.setProgress(100);
                //This is where the facebook sign in will be performed



                //Start the Intent for the next Activity
                //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
                //For now its just navigating to nearby
                //Intent intent = new Intent(Splash.this, makeProfile.class);
                //startActivity(intent);
            }
        }
    });


    if(isLoggedIn()){
        //Start the Intent for the next Activity
        //In the final code this intent will be the 3 tabbed activity (Faves, Nearby, Specials)
        //For now its just navigating to nearby
        Intent intent = new Intent(Splash.this, nearbyPlacesHolder.class);
        startActivity(intent);
        finish();
    }



}

public boolean isLoggedIn(){
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    Profile profile = Profile.getCurrentProfile();
    //Attempts to recognize if the user has logged in before
    if(accessToken !=null && profile !=null) {
            return true;
        }
        else{
            return false;
        }
}
Comments