Skip to content Skip to sidebar Skip to footer

How To Handle Facebook Android Sdk V4.1 Access Token Between Activities

When i go to another activity after successfully logged a user in i can't access to the Facebook Access token AccessToken.getCurrentAccessToken() I got this in the LogCat: {Access

Solution 1:

My solution if you are using the android login button is: To use a fragment and call it inside every activity, you can choose to display it or not depending on what the current activity will be doing, all the fragment's methods will work just fine.

Here is some code:

package com.infoplusplus.anix.currentapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;


/**
 * Created by Anix on 09/04/2015.
 */publicclassLoginFacebookFragmentextendsFragment {
    publicLoginFacebookFragment() {
    }

    privateProfileTracker profileTracker;
    privateAccessTokenTracker tokenTracker;
    privateLoginButton loginButton;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
        tokenTracker = newAccessTokenTracker() {
            @OverrideprotectedvoidonCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
            }
        };
        profileTracker = newProfileTracker() {
            @OverrideprotectedvoidonCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                if (newProfile != null) {
                    //TODO the user has logged out
                } else {
                   //TODO the user may have logged in or changed some of his profile settings
                }
            }
        };
        profileTracker.startTracking();
        tokenTracker.startTracking();
    }

    @OverridepublicViewonCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login_facebook, container, false);
        return rootView;
    }

    privateCallbackManager mCallbackManager;
    privateFacebookCallback<LoginResult> mCallback = newFacebookCallback<LoginResult>() {
        @OverridepublicvoidonSuccess(LoginResult loginResult) {
            //TODO, implement onSuccess
        }

        @OverridepublicvoidonCancel() {
            //TODO, implement onCancel
        }

        @OverridepublicvoidonError(FacebookException e) {
            //TODO, implement onError inorder to handle the errors
        }
    };

    @OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loginButton = (LoginButton) view.findViewById(R.id.btn_facebookLogin);
        //TODO: get some more permissions from the user//loginButton.setReadPermissions("user_friends");
        loginButton.setFragment(this);
        loginButton.registerCallback(mCallbackManager, mCallback);
    }

    @OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @OverridepublicvoidonResume() {
        super.onResume();
        profileTracker.startTracking();
        tokenTracker.startTracking();
    }

    @OverridepublicvoidonPause() {
        super.onPause();
        profileTracker.stopTracking();
        tokenTracker.stopTracking();
    }

    @OverridepublicvoidonDestroy() {
        super.onDestroy();
        tokenTracker.stopTracking();
        profileTracker.stopTracking();
    }
}

Post a Comment for "How To Handle Facebook Android Sdk V4.1 Access Token Between Activities"