1

我知道标题可能很奇怪,但由于我通过将登录周期从 移动到 改变了我的应用程序的生命周期MainActivityLoginActivityFacebookonSessionStateChange完全没有我问他的事情。

登录的生命周期,使用 Facebook,将用户登录到 Facebook,从 fb 获取他的 UID,并使用他的 UID 从我们应用程序的服务器检索他的信息,然后开始下一个活动。但是现在我已经改变了应用程序的生命周期,它甚至没有获得 UID 就开始了下一个活动。

所以这里是onSessionStateChangein 的代码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仍然启动,即使我已经评论过它。并且当activity启动时,当前用户没有更新甚至没有创建,导致经典的crash。

此外,您可能需要其他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 编码一样。

4

0 回答 0