我一步一步地遵循了FB tutorial,但我不知道为什么这不起作用。
我所有的片段都继承自BaseFragment:
public class BaseFragment extends SherlockFragment {
...
protected UiLifecycleHelper _uiHelper;
protected boolean _isResumed = false;
protected static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
protected static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
protected boolean _pendingPublishReauthorization = false;
protected Session.StatusCallback _callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_uiHelper = new UiLifecycleHelper(getSherlockActivity(), _callback);
_uiHelper.onCreate(savedInstanceState);
}
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {}
...(all lifecycle stuff)
}
然后我有一个包含登录/注销按钮的FacebookLoginFragment:
public class FacebookLoginFragment extends BaseFragment {
protected LoginButton _authButton;
protected ProgressBar _loadingBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_login, container, false);
_loadingBar = (ProgressBar) view.findViewById(R.id.login_progressBar);
_authButton = (LoginButton) view.findViewById(R.id.authFacebookButton);
_authButton.setFragment(this);
_authButton.setReadPermissions(Arrays.asList("email", "user_birthday"));
return view;
}
@Override
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
super.onSessionStateChange(session, state, exception);
// LogIn process
}
...
}
最后,我有一个NewsFragment,其中包含供稿的listView。每种饲料都是可共享的。
当我们单击提要的共享按钮时,适配器将调用NewsFragment.publishFeed(提要)方法。
public class NewsFragment extends BaseFragment{
...
protected Feed _feedPendingPublish;
@Override
// => NEVER CALLED
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
super.onSessionStateChange(session, state, exception);
if (_isResumed) {
if (_pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
_pendingPublishReauthorization = false;
if(_feedPendingPublish != null){
publishFeed(_feedPendingPublish);
_feedPendingPublish = null;
}
}
}
}
public void publishFeed(Feed feed){
if(_pendingPublishReauthorization){
return;
}
Session session = Session.getActiveSession();
if (session != null){
// If permission doesn't exist, ask and return. Otherwise, send request
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
_pendingPublishReauthorization = true;
_feedPendingPublish = feed;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(getSherlockActivity(), PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
// DO stuff
}
}
}
问题在于发布授权后未调用NewsFragment回调。 (都不是BaseFragment,也不是FacebookLogin)。
请注意,两个片段都存在于同一活动中。
我正在使用facebook-android-sdk-3.5.2
最佳答案
我终于想通了。
正如许多人所说,这是一个onActivityResult()
问题,但非常恶毒。
登录时,成功调用了FacebookLoginFragment.onSessionStateChange()
,而在允许过程中从未调用NewsFragment.onSessionStateChange()
。
经过几次测试,我认为这是由于静态和动态绑定造成的。我的FacebookLoginfragment
在xml(静态)中直接引用,而我的NewsFragment在源代码中动态添加。
因此,包含两个片段的活动仅将onActivityResult
信号发送到FacebokLoginFragment
。
因此,从未调用触发回调所必需的NewsFragment.onActivityResult()
。
覆盖Activity.onActivityResult()
给了我预期的行为:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getSupportFragmentManager().findFragmentById(R.id.my_fragment_frame).onActivityResult(requestCode, resultCode, data);
}