从主要活动开始活动:
Intent intent = new Intent(MainActivity.this, FacebookActivity.class);
startActivity(intent);
定义处理程序:
public class FacebookActivity extends Activity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_facebook);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile"));
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
但处理程序不会被调用。我在控制台中看到了这一点:
calling onResume for activity
任何想法,有什么问题?在早期版本中,我没有使用单独的活动进行Facebook登录,并且登录处理程序工作正常。奇怪。
答案 0 :(得分:0)
在setContentView
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
答案 1 :(得分:0)
您必须初始化Facebook-Sdk
在此行之前添加这些行
的setContentView(R.layout.activity_facebook);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
答案 2 :(得分:0)
第1步
在初始化CallbackManager
FacebookSdk.sdkInitialize(getApplicationContext());
第2步
然后在onActivityResult()中将登录结果转发给onCreate()中创建的callbackManager
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}