我跟着FB tutorial一步一步,我無法弄清楚爲什麼這不起作用。FB sdk,requestNewPublishPermissions()不會調用任何回調
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
}
...
}
最後我有一個
我所有的碎片從BaseFragment繼承NewsFragment其中包含供稿的列表視圖。每個Feed都是可共享的。 當我們點擊Feed的分享按鈕時,適配器會調用NewsFragment.publishFeed(feed)方法。
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
嘗試增加一個回調到您newPermissionsRequest調用你NewsFragment的onSessionStateChange方法。 –
我已經試過了。內部回調也不被調用。 –