3

我有MainActivity一个LoginActivity. 我创建了一个超类FacebookActivity来保留UiLifecycleHelper对象和所有覆盖方法:

主要活动

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监听器,我不明白我的方法有什么问题。

还有其他方法可以解决这个问题吗?

4

1 回答 1

2

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每次都会调用 .. 导致您的无限循环。

于 2013-05-08T15:25:19.903 回答