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.
source
share