我知道标题可能很奇怪,但由于我已经通过将登录周期从MainActivity
移动到LoginActivity
来改变了应用的生命周期,因此Facebook的onSessionStateChange
绝对没有我问他什么。
登录的生命周期,使用Facebook,将用户登录到Facebook,从fb获取他的UID,并使用他的UID从我们的应用程序的服务器检索他的信息,然后开始下一个活动。但是现在我已经改变了应用程序的生命周期,它启动了下一个活动,甚至没有获得UID。
以下是onSessionStateChange
中LoginActivity
的代码:
private void onSessionStateChange(Session session, SessionState sessionState, Exception exception){
//Make change only if the activity is visible
if(isResumed){
Log.i("facebook", "FB state changed");
FragmentManager fragmentManager = getSupportFragmentManager();
//clear the back stack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); i++){
fragmentManager.popBackStack();
}
if(sessionState.isOpened()){
Log.i("facebook", "FB session opened");
//if the facebook session is opened, display the default behavior and add the uid
//to shared preferences
//Now let's update shared preferences with uid.
final Session requestSession = session;
Request request = Request.newMeRequest(session, new Request.GraphUserCallback(){
@Override
public void onCompleted(GraphUser user, Response response){
if(requestSession == Session.getActiveSession() && user != null){
Log.i("facebook", "Request UID " + user.getId());
GetUserByUIDTask userTask = new GetUserByUIDTask(getParent());
userTask.execute(new User(user.getId()));
//Get and update the current user from the database
try{
MainActivity.sCurrentUser = userTask.get();
Log.i("facebook", MainActivity.sCurrentUser.firstName
+ "'s UID is " + MainActivity.sCurrentUser.uid);
updateCurrentUserSettings();
}catch (InterruptedException e){
//TODO: Handle and display error
Log.e("facebook", "error getting user from uid");
return;
}catch (ExecutionException e){
//TODO: Handle and display error
Log.e("facebook", "error getting user from uid");
return;
}
Log.d("facebook", "Displaying Main Activity");
//startActivity(new Intent(getParent(), MainActivity.class));
}else{
Log.e("facebook", "Error when making newMeRequest");
}
}
});
request.executeAsync();
}else if(sessionState.isClosed()){
Log.i("myapp", "closing session");
//Remove the current uid to prevent unauthorized authentication and GCM bind
getSharedPreferences(userPrefsFile,0).edit().clear().commit();;
try {
GoogleCloudMessaging.getInstance(this).unregister();
} catch (IOException e) {
//TODO: handle
e.printStackTrace();
}
}
}
}
正如你所看到的,我应该得到很多Log
输出,告诉我会话正在打开,并且MainActivity
不应该被启动(因为我为了测试目的而评论它) 。但我实际上得到绝对没有日志输出,MainActivity
仍然会启动,即使我已经评论过它。当活动开始时,当前用户不会更新甚至创建,从而导致典型的崩溃。
此外,您可能还需要其他LoginActivity
的facebook相关方法:
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback sessionStatusCallback = 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);
//bind the fb session callback listener to the ui helper and call its lifecycle method
uiHelper = new UiLifecycleHelper(this, sessionStatusCallback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
RegisterChoicesFragment registerFragment = new RegisterChoicesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.login_container,
registerFragment).commit();
}//end onCreate
@Override
public void onResume(){
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause(){
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy(){
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
protected void onResumeFragments(){
super.onResumeFragments();
Session session = Session.getActiveSession();
if(session != null && session.isOpened()){
//If the current facebook session exists and is opened, make sure that the autheticated
//UI is selected
startActivity(new Intent(this, LovRMain.class));
}
}
所以在这里,我完全不知道是什么让Facebook API表现得像 IT 想要的那样,而且我已经到了这样的地步,我意识到我讨厌像facebook一样编码我讨厌为IE编码。