我正在尝试将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();
}
}