我有MainActivity
和LoginActivity
。 我创建了一个超类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
我得到一个循环:
-
onSessionStateChange
事件监听器触发了,因为我清除了会话 -
LoginActivity
启动 -
FacebookActivity.onCreate()
是由被称为LoginActivity.onCreate()
-
onSessionStateChange
被再次调用,并返回到2
我不知道到底是什么事件触发Session.StatusCallback
听众,我不明白什么是错用我的方法。
任何其他方式来处理呢?