实施Facebook登录与UiLifecycleHelper(Implementing Facebo

2019-09-02 02:34发布

我有MainActivityLoginActivity 。 我创建了一个超类FacebookActivity保持UiLifecycleHelper对象和所有的重载方法:

MainActivity

public class MainActivity extends FacebookActivity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }
}

LoginActivity(基本上是初始屏幕与facebook登录按钮):

public class LoginActivity extends FacebookActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login_activity);
    }

}

FacebookActivity实现所有的UiLifecycle方法:

public class FacebookActivity extends SherlockFragmentActivity {

    private UiLifecycleHelper uiHelper;
    private Session.StatusCallback statusCallback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    public FacebookActivity() {
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(this, statusCallback);
        uiHelper.onCreate(savedInstanceState);
    }

    @Override
    protected 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
    protected void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
        uiHelper.onSaveInstanceState(outState);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(Consts.TAG, "Logged in...");
            setCookies();
            Intent intent = new Intent(this, MainActivity.class);
            this.startActivity(intent);
        } else if (state.isClosed()) {
            Log.i(Consts.TAG, "Logged out...");
            Intent intent = new Intent(this, LoginActivity.class);
            this.startActivity(intent);
        }
    }
}

我认为这将是一个好主意,所有的活动课,将继承FacebookActivity这样我会处理所有的会话管理的一个代码。

不幸的是这并不能很好地工作,其中一个例子是,当我注销通过MainActivity我得到一个循环:

  1. onSessionStateChange事件监听器触发了,因为我清除了会话
  2. LoginActivity启动
  3. FacebookActivity.onCreate()是由被称为LoginActivity.onCreate()
  4. onSessionStateChange被再次调用,并返回到2

我不知道到底是什么事件触发Session.StatusCallback听众,我不明白什么是错用我的方法。

任何其他方式来处理呢?

Answer 1:

UiLifecycleHelper明明做了很多相关的东西Session类。 这是的一部分onCreate方法:

public void onCreate(Bundle savedInstanceState) {
    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(activity, null, callback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(activity);
        }
        Session.setActiveSession(session);
    }

    [...]
}

会话变化,所以Session.StatusCallback被称为每次..导致您的无限循环。



文章来源: Implementing Facebook login with UiLifecycleHelper