Implementing Facebook login with UiLifecycleHelper

蓝咒 提交于 2019-12-06 08:13:48

问题


I have MainActivity and a LoginActivity. I created a super class FacebookActivity to keep the UiLifecycleHelper object and all of the overriding methods:

MainActivity

public class MainActivity extends FacebookActivity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {

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

LoginActivity (basically a splash screen with facebook login button):

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 to implement all of the UiLifecycle methods :

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);
        }
    }
}

I thought it would be a good idea that all activity classes would inherit from FacebookActivity this way I will have one code that handles all of the session management.

Unfortunately this doesn't work very well, one example is when I log out through MainActivity I get a loop :

  1. onSessionStateChange event listener fires up since I cleared the session
  2. LoginActivity is started
  3. FacebookActivity.onCreate() is called from LoginActivity.onCreate()
  4. onSessionStateChange is called again and go back to 2

I don't exactly know what events trigger the Session.StatusCallback listener, and I don't understand whats wrong with my approach.

Any other way to approach this ?


回答1:


The UiLifecycleHelper obviously does a lot of things related to the Session class. This is a part of the onCreate method:

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);
    }

    [...]
}

The session change, so the Session.StatusCallback is called every time.. causing your infinite loop.



来源:https://stackoverflow.com/questions/16403566/implementing-facebook-login-with-uilifecyclehelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!