Sharing a global facebook object in Android actions

I am creating a global Facebook object (from android-facebook-sdk) to be able to share it with my actions:

public class GlobalVars extends Application {

    public static final String APP_ID = "123456789";    
    public Facebook facebook = new Facebook(APP_ID);

}

In one of the actions, I add LoginButton, as shown in the examples:

public class FacebookActivity extends Activity {

    private LoginButton mLoginButton;

    private Facebook mFacebook;    
    private AsyncFacebookRunner mAsyncRunner;    

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mFacebook = ((GlobalVars)getApplicationContext()).facebook;

        setContentView(R.layout.facebook);
        mLoginButton = (LoginButton) findViewById(R.id.login);
        mText = (TextView) FacebookActivity.this.findViewById(R.id.txt);

        mAsyncRunner = new AsyncFacebookRunner(mFacebook);

        SessionStore.restore(mFacebook, this);
        SessionEvents.addAuthListener(new SampleAuthListener());
        SessionEvents.addLogoutListener(new SampleLogoutListener());
        mLoginButton.init(this, mFacebook);

    }

    ...

}

Now when I log in and move on to another action using the same Facebook object:

public class MainActivity extends WhipemActivity {


    private Facebook mFacebook;    
    private AsyncFacebookRunner mAsyncRunner;   

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mFacebook = ((GlobalVars)getApplicationContext()).facebook;

    }

    ....

}

and run the query, for example:

mAsyncRunner = new AsyncFacebookRunner(mFacebook);
mAsyncRunner.request("me/friends", new FriendRequestListener());

I get

Facebook Error: Error validating access token.

What happened? Is this the right way to have a global Facebook object?

Thank.

+3
source share
1 answer

jul, in your new action, you need to restore the access token received during the login.

SessionStore.restore(mFacebook, this);

SessionStore saves and restores the access token to / from SharedPreferences through the created SharedPreferences object.

SessionStore.restore() , SharedPreferences, Facebook.

" Facebook" SessionStore, , SessionStore.

facebook-android-sdk/examples/simple

+3

All Articles