51

我正在尝试将 Facebook 集成到我的 Android 应用程序中。使用 Facebook 登录按钮,我尝试登录。当我第一次安装该应用程序时,它运行良好并成功登录。当我使用该按钮注销,然后尝试再次登录时,它会显示权限对话框,但SessionState返回状态已关闭。有什么建议么?

这是我的代码:

public class LoginActivity extends FragmentActivity {

 // private Login login;
 private boolean isResumed = false;
 private BirthDayComp app;
 private int Login = 0;
 private int LogOut = 1;
 private int Count = LogOut + 1;
 private boolean loged = false;
 private Fragment[] fragments = new Fragment[Count];
 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
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.login_fragment);
  uiHelper = new UiLifecycleHelper(this, callback);
  uiHelper.onCreate(savedInstanceState);

  FragmentManager fm = getSupportFragmentManager();
  fragments[Login] = fm.findFragmentById(R.id.loginfb);
  fragments[LogOut] = fm.findFragmentById(R.id.logoutfb);
  FragmentTransaction transaction = fm.beginTransaction();
  transaction.commit();
  /*if (savedInstanceState == null) {
    login = new Login();
    getSupportFragmentManager()
    .beginTransaction()
    .add(android.R.id.content, login)
    .commit();
  } else {
    login = (Login) getSupportFragmentManager()
            .findFragmentById(android.R.id.content);
  }*/
 }


 @Override
 public void onResume() {
  super.onResume();
  uiHelper.onResume();
  isResumed = true;
 }

 @Override
 public void onPause() {
  super.onPause();
  uiHelper.onPause();
  /* Session session = Session.getActiveSession();
   session.removeCallback(callback);*/
  isResumed = false;
 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  // uiHelper.onActivityResult(requestCode, resultCode, data);
  //  super.onActivityResult(requestCode, resultCode, data);
  Session session = Session.getActiveSession();
  int sanitizedRequestCode = requestCode % 0x10000;
  session.onActivityResult(this, sanitizedRequestCode, resultCode, data);
 }

 @Override
 public void onDestroy() {
  super.onDestroy();
  uiHelper.onDestroy();
 }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  uiHelper.onSaveInstanceState(outState);
 }

 @Override
 protected void onResumeFragments() {
  super.onResumeFragments();
  Session session = Session.getActiveSession();

  if (session != null && session.isOpened()) {
   // if the session is already open, try to show the selection fragment
   /*if(app.getFb()!=true){
    app.setFb(true);
   }*/

   showFragment(Login, false);
  } else {
   // otherwise present the splash screen and ask the user to login.
   //app.setFb(false);
   showFragment(Login, false);
  }
 }

 private void onSessionStateChange(Session session, SessionState state, Exception exception) {
  if (isResumed) {
   FragmentManager manager = getSupportFragmentManager();
   int backStackSize = manager.getBackStackEntryCount();
   for (int i = 0; i < backStackSize; i++) {
    manager.popBackStack();
   }
   // check for the OPENED state instead of session.isOpened() since for the
   // OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
   session = Session.getActiveSession();

   state = session.getState();
   if (state.equals(SessionState.OPENED)) {
    this.loged = true;
    showFragment(Login, false);

   } else if (state.isClosed()) {
    this.loged = false;
    System.gc();
    //session.closeAndClearTokenInformation();
    // showFragment(Login, false);
   }
  }
 }

 private void showFragment(int fragmentIndex, boolean addToBackStack) {

  FragmentManager fm = getSupportFragmentManager();
  FragmentTransaction transaction = fm.beginTransaction();
  for (int i = 0; i < fragments.length; i++) {
   if (i == fragmentIndex) {
    transaction.show(fragments[i]);
   } else {
    transaction.hide(fragments[i]);
   }
  }
  if (addToBackStack) {
   transaction.addToBackStack(null);
  }
  transaction.commit();
 }
}
4

1 回答 1

0

状态对我来说没问题。我根据需要有打开和关闭状态。

于 2018-03-14T09:38:50.427 回答