Note that there are some explanatory texts on larger screens.

plurals
  1. POonSessionStateChange does whatever it wants
    text
    copied!<p>I know the title may be odd, but since I've changed my app's lifecycle by moving the login cycle from the <code>MainActivity</code> to the <code>LoginActivity</code>, Facebook's <code>onSessionStateChange</code> does ABSOLUTELY NOTHING of what i ask him.</p> <p>The login's lifecycle, using Facebook, logs the user to Facebook, gets his UID from fb, and retrieve his infos from our app's server using his UID, and then starts the next activity. But now that I've changed the app's lifecycle, it starts the next activity without even getting the UID.</p> <p>So here is the code for the <code>onSessionStateChange</code> in the <code>LoginActivity</code> :</p> <pre><code>private void onSessionStateChange(Session session, SessionState sessionState, Exception exception){ //Make change only if the activity is visible if(isResumed){ Log.i("facebook", "FB state changed"); FragmentManager fragmentManager = getSupportFragmentManager(); //clear the back stack for(int i = 0; i &lt; fragmentManager.getBackStackEntryCount(); i++){ fragmentManager.popBackStack(); } if(sessionState.isOpened()){ Log.i("facebook", "FB session opened"); //if the facebook session is opened, display the default behavior and add the uid //to shared preferences //Now let's update shared preferences with uid. final Session requestSession = session; Request request = Request.newMeRequest(session, new Request.GraphUserCallback(){ @Override public void onCompleted(GraphUser user, Response response){ if(requestSession == Session.getActiveSession() &amp;&amp; user != null){ Log.i("facebook", "Request UID " + user.getId()); GetUserByUIDTask userTask = new GetUserByUIDTask(getParent()); userTask.execute(new User(user.getId())); //Get and update the current user from the database try{ MainActivity.sCurrentUser = userTask.get(); Log.i("facebook", MainActivity.sCurrentUser.firstName + "'s UID is " + MainActivity.sCurrentUser.uid); updateCurrentUserSettings(); }catch (InterruptedException e){ //TODO: Handle and display error Log.e("facebook", "error getting user from uid"); return; }catch (ExecutionException e){ //TODO: Handle and display error Log.e("facebook", "error getting user from uid"); return; } Log.d("facebook", "Displaying Main Activity"); //startActivity(new Intent(getParent(), MainActivity.class)); }else{ Log.e("facebook", "Error when making newMeRequest"); } } }); request.executeAsync(); }else if(sessionState.isClosed()){ Log.i("myapp", "closing session"); //Remove the current uid to prevent unauthorized authentication and GCM bind getSharedPreferences(userPrefsFile,0).edit().clear().commit();; try { GoogleCloudMessaging.getInstance(this).unregister(); } catch (IOException e) { //TODO: handle e.printStackTrace(); } } } } </code></pre> <p>As you can see, i should get a lot of <code>Log</code> output, telling me that the session is being opened, and the <code>MainActivity</code> should not be started (since i commented it for testing purposes). But i actually get <strong>absolutely</strong> no log output, and the <code>MainActivity</code> still gets launched, even though I've commented it. And when the activity is started, the current user is not updated or even created, leading to a classic crash.</p> <p>Also you may need the other <code>LoginActivity</code>'s facebook related methods :</p> <pre><code>private boolean isResumed = false; private UiLifecycleHelper uiHelper; private Session.StatusCallback sessionStatusCallback = 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); //bind the fb session callback listener to the ui helper and call its lifecycle method uiHelper = new UiLifecycleHelper(this, sessionStatusCallback); uiHelper.onCreate(savedInstanceState); setContentView(R.layout.activity_login); RegisterChoicesFragment registerFragment = new RegisterChoicesFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.login_container, registerFragment).commit(); }//end onCreate @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 public void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); } @Override protected void onResumeFragments(){ super.onResumeFragments(); Session session = Session.getActiveSession(); if(session != null &amp;&amp; session.isOpened()){ //If the current facebook session exists and is opened, make sure that the autheticated //UI is selected startActivity(new Intent(this, LovRMain.class)); } } </code></pre> <p>So here it is, I have absolutely no idea of what makes Facebook API behave like <strong>IT</strong> wants, and I've come to the point where i realize that i hate coding with facebook as much as i hate coding for IE.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload