我将Facebook登录集成在我创建的原生Android应用程序中。打开Facebook Session的UiLifecycleHelper类之前工作正常,但突然会话未被打开且Session.StatusCallback未被触发且onSessionStateChange()方法没有被调用。
public class SessionActivity extends FragmentActivity {
private static final int SIGNUP = 0;
private static final int CITYLIST = 1;
private static final int FRAGMENT_COUNT = CITYLIST+1;
private boolean isResumed = false;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
Toast.makeText(getApplicationContext(), "UiLifecycle is called", Toast.LENGTH_SHORT).show();
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
// Toast.makeText(getApplicationContext(), "UiLifecycle is not called", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_session);
FragmentManager fm = getSupportFragmentManager();
fragments[SIGNUP] = fm.findFragmentById(R.id.SignupFragment);
fragments[CITYLIST] = fm.findFragmentById(R.id.CityListFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@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 showFragment(int fragmentIndex, boolean addToBackStack) {
Toast.makeText(getApplicationContext(), "showFragment is called", Toast.LENGTH_SHORT).show();
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();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
Toast.makeText(getApplicationContext(), "onSessionStateChange is called", Toast.LENGTH_SHORT).show();
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.equals(SessionState.OPENED)) {
// If the session state is open:
// Show the authenticated fragment
showFragment(CITYLIST, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SIGNUP, false);
}
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
Toast.makeText(getApplicationContext(), "onResumeFragments is called" + session.isOpened(), Toast.LENGTH_SHORT).show();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(CITYLIST, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SIGNUP, false);
}
}
}
问题
为什么UILifeCycleHelper助手没有打开新的FB会话并打开目标片段(CityListFragment)?
信息
代码工作正常之前,会话打开并且令牌已创建,并且在我进行了一些编辑后它无效。
答案 0 :(得分:0)
我试图在我启动SessionActivity的另一个活动中使用Facebook登录按钮。我想在SessionActivity中使用fb登录按钮的原因是我希望用户单击另一个活动中的登录按钮进入SessionActivity.I通过使用SessionActivity的onCreate()方法中的Fb loginbutton解决了这个问题,隐藏了它并使用performclick()方法执行了点击(因为用户已经在其他活动中按下了自定义按钮i用过)。现在新的会话已经开启。
LoginButton loginBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
loginBtn = (LoginButton) findViewById(R.id.fb_profile_activity_button);
loginBtn.setVisibility(View.INVISIBLE);
loginBtn.performClick();
FragmentManager fm = getSupportFragmentManager();
fragments[SIGNUP] = fm.findFragmentById(R.id.signup_fragment);
fragments[CITYLIST] = fm.findFragmentById(R.id.citylist_fragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}