title: Android facebook - Why the permission is "disappear" after the user cancel in permission dialog?
body: <p>The workflow is like this:</p>
  
  <p>user click on login-> after login success-> prompt permission dialog, if accept , logined, if cancel, open the box again next time</p>
  
  <p>The problem is , for the first time the permission dialog has request the email permission, however, if the user click on cancel and open the box later, the email permission seems no longer exist, it just ask user for their personal info. </p>
  
  <p>How to fix this? Thanks</p>
  
  <p>Update: I try to fix the code, however, it throw exception and it seems related to the flow logic. How to fix this? I will provide the very detail of my code , thanks for helping.</p>
  
  <p><strong>The modification:</strong></p>
  
  <ol>
  <li><p>Added <code>private final static List&lt;String&gt; PERMISSIONS = Arrays.asList("email");</code></p></li>
  <li><p>update onclickLogin()</p>
  
  <pre><code>private void onClickLogin() {
  Session session = Session.getActiveSession();
  List&lt;String&gt; permissions = session.getPermissions();
  
  Log.d("test1","p:" + permissions);
  Log.d("test1","session open" + session.isOpened());
  
  if (!session.isOpened() &amp;&amp; !session.isClosed()) {
      Log.d("test1","test1");
      session.openForRead(new Session.OpenRequest(this).setPermissions("email").setCallback(statusCallback));
  } else if(!isSubset(PERMISSIONS, permissions)) {
      Log.d("test1","test2");
      Session.NewPermissionsRequest newPermissions = new Session.NewPermissionsRequest(this, PERMISSIONS);
      session.requestNewPublishPermissions(newPermissions); 
  } else {
      Log.d("test1","test3");
      Session.openActiveSession(getActivity(), this, true, statusCallback);
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>added isSubset()</p></li>
  </ol>
  
  <p><strong>The log cat output</strong></p>
  
  <pre><code>03-09 14:30:29.301: D/test1(23828): p:[]
  03-09 14:30:29.301: D/test1(23828): session openfalse
  03-09 14:30:29.301: D/test1(23828): test1
  03-09 14:30:42.654: D/test1(23828): p:[]
  03-09 14:30:42.654: D/test1(23828): session openfalse
  03-09 14:30:42.654: D/test1(23828): test2
  </code></pre>
  
  <p>The exception</p>
  
  <pre><code>03-09 14:30:42.694: E/AndroidRuntime(23828): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has been closed.
  </code></pre>
  
  <p>Notes: it seems after clicking cancel and open the box for the second time, the session is still not opened so I removed the Session.isopened checking. Otherwise it only fall into the third condition</p>
  
  <p>Here is the full code of the Class:</p>
  
  <pre><code>public class Home extends Fragment implements LoginListener {
      private final static String TAG = "Home";
      public View rootView;
      public ImageView HomeBg;
      public ImageView buttonLoginLogout;
      public TextView chi;
      public TextView eng;
      public ColorStateList oldColor;
      public SharedPreferences prefs;
      public EasyTracker tracker = null;
      public SharedPreferences.Editor editor;
      public ProgressDialog pd;
      public Home ctx;
  
      //Facebook login
      private Session.StatusCallback statusCallback = new SessionStatusCallback();
      private final static List&lt;String&gt; PERMISSIONS = Arrays.asList("email");
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState); 
          ctx = this;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          try{
              // fix for disable the hiding action bar animation
              getActivity().getActionBar().getClass().getDeclaredMethod("setShowHideAnimationEnabled", boolean.class).invoke(getActivity().getActionBar(), false);
          } catch (Exception exception){
              // animation will be run if not support
          }
  
          getActivity().getActionBar().hide();
  
          tracker = EasyTracker.getInstance(getActivity());
  
          prefs = getActivity().getSharedPreferences("userInfo", 0);
          editor = prefs.edit();
  
          rootView = inflater.inflate(R.layout.home, container, false);
          buttonLoginLogout = (ImageView) rootView.findViewById(R.id.home_connectFB);
          eng = (TextView) rootView.findViewById(R.id.btn_eng);
          chi = (TextView) rootView.findViewById(R.id.btn_chi);
  
          eng.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View arg0) {
                  if (!Utility.getLocale(getActivity()).equals("en")) {
                      tracker.send(MapBuilder.createEvent("menu_click","language", "switchEN", null).build());
                      chi.setTextColor(oldColor);
                      eng.setTextColor(getActivity().getResources().getColor(android.R.color.white));
                      MyApp.updateLanguage(getActivity().getApplicationContext(), "en");
                      refreshAfterLocaleChanged("en");
                  }
              }
          });
  
          chi.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View arg0) {
                  if (!Utility.getLocale(getActivity()).equals("zh")) {
                      tracker.send(MapBuilder.createEvent("menu_click","language", "switchTC", null).build());
                      eng.setTextColor(oldColor);
                      chi.setTextColor(getActivity().getResources().getColor(android.R.color.white));
                      MyApp.updateLanguage(getActivity().getApplicationContext(), "zh");
                      refreshAfterLocaleChanged("zh");
                  }
              }
          });
  
          if (Utility.getLocale(getActivity()).equals("zh")) {
              chi.setTextColor(getActivity().getResources().getColor(
                      android.R.color.white));
              oldColor = eng.getTextColors();
          } else {
              eng.setTextColor(getActivity().getResources().getColor(
                      android.R.color.white));
              oldColor = chi.getTextColors();
  
          }
  
          //Facebook login
          Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
  
          Session session = Session.getActiveSession();
          if (session == null) {
              if (savedInstanceState != null) {
                  session = Session.restoreSession(getActivity(), null, statusCallback, savedInstanceState);
              }
              if (session == null) {
                  session = new Session(getActivity());
              }
              Session.setActiveSession(session);
              if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                 // session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("basic_info","email")).setCallback(statusCallback));
                  session.openForRead(new Session.OpenRequest(this).setPermissions("email").setCallback(statusCallback));
              }
          }
  
         updateView();
  
         return rootView;
      }
  
      public void refreshAfterLocaleChanged(String lang){
          SharedPreferences langPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
          Editor editor = langPrefs.edit();
          editor.putString("lang",lang).commit();
  
          Intent intent = getActivity().getIntent();
          intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
          getActivity().finish();
          getActivity().overridePendingTransition(0, 0);
          startActivity(intent);
      }
  
      @Override
      public void onStart() {
          super.onStart();
          Session.getActiveSession().addCallback(statusCallback);
          tracker.set(Fields.SCREEN_NAME, "hk7app/CX/" + Utility.getLocale(getActivity()) + "/Landing Page");
          tracker.send(MapBuilder.createAppView().build());
      }
  
      @Override
      public void onStop() {
          super.onStop();
  
          if (pd != null)
              pd.dismiss();
  
          Session.getActiveSession().removeCallback(statusCallback);
          EasyTracker.getInstance(getActivity()).activityStop(getActivity());
      }
  
      @SuppressWarnings("deprecation")
      private void updateView() {
          Session session = Session.getActiveSession();
          if (session.isOpened()) {
               final String token = session.getAccessToken();
               Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                      @Override
                      public void onCompleted(GraphUser user,Response response) {
                          // TODO Auto-generated method stub
                          if (user != null) {
                              if (!user.getId().equals(prefs.getString("fbId",""))) {
                                  editor.putString("fbId", user.getId());
                                  editor.putString("fName", user.getFirstName());
                                  editor.putString("lName", user.getLastName());
                                  if (user.asMap() != null &amp;&amp; user.asMap().get("email") != null) {
                                      editor.putString("email", user.asMap().get("email").toString());
                                  }
                                  editor.commit();
                              }
                              if (prefs.getString("memId","").equals("") || prefs.getString("token","").equals("") || !user.getId().equals(prefs.getString("fbId",""))){
                                  pd = ProgressDialog.show(getActivity(), getActivity().getResources().getString(R.string.sys_info),getActivity().getResources().getString(R.string.loading));
                                  new APIHandler(getActivity(),tracker,"login",pd).execute(ctx,token);
                              } else {
                                  onLoginComplete("","");
                              }
                          }
                      }
               });                  
          } else {
              buttonLoginLogout.setImageResource(R.drawable.landing_btn_connect_facebook);
              buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View view) { onClickLogin(); }
              });
          }
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          Session session = Session.getActiveSession();
          Session.saveSession(session, outState);
      }
  
      private void onClickLogin() {
          tracker.send(MapBuilder.createEvent("menu_click","Fbconnect","connectFB_" + Utility.getLocale(getActivity()),null).build());
          Session session = Session.getActiveSession();
          List&lt;String&gt; permissions = session.getPermissions();// to get the session permissions
  
          Log.d("test1","p:" + permissions);
          Log.d("test1","session open" + session.isOpened());
  
          if (!session.isOpened() &amp;&amp; !session.isClosed()) {
              Log.d("test1","test1");
              session.openForRead(new Session.OpenRequest(this).setPermissions("email").setCallback(statusCallback));
          } else if(!isSubset(PERMISSIONS, permissions)) {
              Log.d("test1","test2");
              Session.NewPermissionsRequest newPermissions = new Session.NewPermissionsRequest(this, PERMISSIONS);
              session.requestNewPublishPermissions(newPermissions); 
          } else {
              Log.d("test1","test3");
              Session.openActiveSession(getActivity(), this, true, statusCallback);
          }
      }
  
      //this function is to check if the user has a specific permission within the session
      private boolean isSubset(Collection&lt;String&gt; subset, Collection&lt;String&gt; superset) {
          for (String string : subset) {
              if(!superset.contains(string))
                  return false;
          }
          return true;
      }
  
      private class SessionStatusCallback implements Session.StatusCallback {
          @Override
          public void call(Session session, SessionState state, Exception exception) {
              Log.d(TAG,"SessionStatusCallback");
              if (exception != null) {
                  Log.d(TAG,""+exception);
  
                  if (!session.isClosed()) {
                     session.closeAndClearTokenInformation(); //reset session
                     Session.setActiveSession(null);
                  }
  
                  if (exception instanceof FacebookAuthorizationException){
                      new AlertDialog.Builder(getActivity())
                      .setTitle(ctx.getResources().getString(R.string.sys_info))
                      .setMessage(R.string.facebook_permission_missing)
                      .setPositiveButton(ctx.getResources().getString(R.string.close), null)
                      .show();
                  }
              } else {
                  updateView();
              }
          }
      }
  
      @Override
      public void onResume() {
          super.onResume();
          AppEventsLogger.activateApp(getActivity(),getResources().getString(R.string.app_id));
      }
  
      @Override
      public void onLoginComplete(String memberId, String token) {    
          if (pd != null)
              pd.dismiss();
  
          if (!memberId.equals("") &amp;&amp; !token.equals("")) {
              editor.putString("memId", memberId);
              editor.putString("token", token);
              editor.commit();
          }
  
          buttonLoginLogout.setImageResource(R.drawable.landing_btn_take_a_selfie);
          buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
                   tracker.send(MapBuilder.createEvent("menu_click","TakeSelfie","Selfie_now_" + Utility.getLocale(getActivity()),null).build());
                  ((LandingPage)getActivity()).tabHost.setCurrentTab(2);
              }
          });
      }
  
      @Override
      public void onLoginFailure(String errorMsg) {
          if (pd != null)
              pd.dismiss();
  
          Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_LONG).show();
      }
  
  }
  </code></pre>