我有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
我得到一个循环:
onSessionStateChange
事件监听器在我清除会话后启动LoginActivity
开始FacebookActivity.onCreate()
被称为LoginActivity.onCreate()
onSessionStateChange
再次调用并返回 2
我不完全知道什么事件触发了Session.StatusCallback
监听器,我不明白我的方法有什么问题。
还有其他方法可以解决这个问题吗?