我正在开发一个拥有FB授权的应用程序,我有一个主要活动(片段活动),用户可以通过他的名字/密码或FB登录,当他点击“登录FB”按钮时将被移动到FB-auth片段,现在它可以正常工作非常好但不知何故 Session.StatusCallback 永远不会被调用,我已经注意到这一点,当我试图从FB获取用户数据。那么它可能是什么原因?
这是我的FBFragment代码:
public class FragmentLoginFacebook extends Fragment {
private static final String TAG = FragmentLoginFacebook.class.getSimpleName();
private UiLifecycleHelper uiHelper;
private final List<String> permissions;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_for_fragments, container, false);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(getActivity(), null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(getActivity());
}
Session.setActiveSession(session);
}
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(permissions));
} else {
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
return view;
}
public FragmentLoginFacebook() {
permissions = Arrays.asList("user_status", "email", "user_location", "user_birthday", "user_likes", "publish_actions");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), statusCallback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(getActivity(), "User is " + user.getUsername(), Toast.LENGTH_LONG).show();
}
}
});
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
//--> Never get Called
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
//Cause i'd never seen a Login/Logout logcat messages from onSessionStateChange
};
}
UPD: 我的活动课
public class ActivityLogin extends FragmentActivity {
private Fragment mCurrentFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_fragments);
if (savedInstanceState == null) {
mCurrentFragment = new FragmentLoginMain();
if(getSupportFragmentManager().findFragmentByTag("login") == null){
getSupportFragmentManager().beginTransaction().add(R.id.fragmentLayout, mCurrentFragment, "login").commit();
}
}
}
}
UPD#2 通过 Roger Alien 回答我在活动中添加了
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && (requestCode & 0xFFFF) == 0xFACE) { // if it was request to authorize facebook user
for (Fragment fragment : getSupportFragmentManager().getFragments()) { // transmit to all fragments? is it safe..? // TODO check logic
if (fragment != null) {
fragment.onActivityResult(requestCode & 0xffff, resultCode, data);
}
}
}
}
仍然没有工作!