title: Android - Clear Facebook access token
body: <p>I have a <code>SettingsActivity</code> where there are several options including finding Facebook friends and logging out. So when a user chooses to find her Facebook friends, she will be sent to another activity where I let her logging in with her Facebook account and save her access token. And then when she chooses to log out, her access token will be cleared. The problem is,  my <code>logout</code> method is not written in the same activity with the one I created the session, so when I tried this:</p>
  
  <pre><code>    Session session = Session.getActiveSession();
      if (!session.isClosed()) {
          session.closeAndClearTokenInformation();
      }
  </code></pre>
  
  <p>Logcat points <code>NullPointerException</code>. Then I tried:</p>
  
  <pre><code>Session.setActiveSession(null);
  </code></pre>
  
  <p>And that doesn't work either (friends of the user logged in earlier are still shown, instead of asking the new user to log in).</p>
  
  <p>So what should I do to clear the obtained token? Thank you in advance.</p>
  
  <p>-EDIT-</p>
  
  <p>I think there's something wrong with the activity which I use to open the session. I followed Facebook's <a href="https://github.com/facebook/facebook-android-sdk/tree/master/samples/GraphApiSample" rel="nofollow">GraphAPISample</a> and this is what I have done:</p>
  
  <pre><code>public class FacebookFriendsActivity extends FragmentActivity{
      private BroadcastReceiver broadcast;
      public static Session session;
      private boolean pendingRequest;
      static final String PENDING_REQUEST_BUNDLE_KEY = "PendingRequest";
  
      private DatabaseHandler db;
      private FindFacebookFriends task;
      private ProgressBar progress;
      private TextView text;
      private Button retryBtn;
      private ListView userLayout;
      private ArrayList&lt;User&gt; userList;
      private FollowAdapter adapter;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.facebook_friends);
          // ... set views 
  
          session = createSession();
          Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
  
          showFriends();
      }
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (session.onActivityResult(this, requestCode, resultCode, data) &amp;&amp;
                  pendingRequest &amp;&amp;
                  session.getState().isOpened()) {
              showFriends();
          }
      }
  
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
  
          pendingRequest = savedInstanceState.getBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
  
          outState.putBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
      }
  
      private void showFriends(){
          task = new FindFacebookFriends();
          task.execute(session.getAccessToken());
  
      }
  
      private Session createSession() {
          Session activeSession = Session.getActiveSession();
          if (activeSession == null || activeSession.getState().isClosed()) {
              activeSession = new Session.Builder(this).setApplicationId(getString(R.string.fb_app_id)).build();
              Session.setActiveSession(activeSession);
              new SaveFacebookId().execute(activeSession.getAccessToken());
          }
  
          return activeSession;
      }
  }
  </code></pre>
  
  <p>When I try putting a <code>Toast</code> in <code>createSession()</code> to check the state of the created <code>activeSession</code>, it always shows that the session is closed. It's weird because the <code>showFriends()</code> method is still triggered and even after I delete and install the app again, it still show the same friend list. I'm really confused here.</p>