好吧,我找到了一個工作解決方案。
此代碼確保您將被轉發至MainActivity
,之後,LoginActivity
將從後退堆棧中移除(後退按鈕將關閉應用程序)。
Hwoever,還是有一個問題:如果我關閉應用程序,從我Facebook
帳戶中刪除該應用程序,並重新安裝它,我會再轉發到MainActivity
所以我不會被迫再次登錄。如果我關閉了應用程序並再次打開,LoginActivity
顯示正確。
如果我調試這種情況,萬物完美的作品。我怎樣才能避免這個問題?
public class LoginActivity extends Activity {
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
@Override
public void onResume() {
super.onResume();
isResumed = true;
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (isResumed) {
if (session != null && session.isOpened()) {
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
}
}