1

I am stuck with this error for last 8 hours after a lot of debugging I have found that data returned via Intent is null inside my onActivityResult method.

This is the code that I use to login.

signInWithFacebook(); method is called via button in my activity,

 Session mCurrentSession;
 SessionTracker mSessionTracker; 

 private void signInWithFacebook() {

    mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
        }
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    mCurrentSession = mSessionTracker.getSession();


    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        mSessionTracker.setSession(null);
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        Session.setActiveSession(session);
        mCurrentSession = session;

        System.out.println("session closed or null"+mCurrentSession);
    }
    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(loginScreen.this);

        if (openRequest != null) {
            openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            mCurrentSession.openForRead(openRequest);
            //mCurrentSession.openForPublish(openRequest);
            System.out.println("1111");
        }
    }else {
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
              @Override
              public void onCompleted(GraphUser user, Response response) {

                    System.out.println("..reached here...");
                    String json=user.getInnerJSONObject().toString();

                    try {
                        JSONObject fbJson=new JSONObject(json);
                        Email=fbJson.getString("email");
                        Name=fbJson.getString("name");
                        Id=fbJson.getString("id");
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    System.out.println("22222");

                    if(mProgressDialog1!=null)
                    mProgressDialog1.dismiss();

                    DisplayrogressDialog("Signing in ");
                    new LoginThread(Email, "","facebook",Id).start();

                    /*Intent mIntent = new Intent(loginScreen.this, SelectCourseActivity.class);
                    mIntent.putExtra("fbid", Id);
                    mIntent.putExtra("fbemail", Email);
                    mIntent.putExtra("fbname", Name);
                    mIntent.putExtra("signupvia", "facebook");
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(mIntent);*/

              }
            });
    }
}

And then onActivityResult method is called with data value as null at the first time but next time when I click the same button it returns data with some values and I am able to login, This doesn't happen at certain places but on some devices

public void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);

              if(mProgressDialog1!=null)
              mProgressDialog1.setMessage("Connecting to Studycopter...");

              else
              DisplayrogressDialogFB("Connecting to Studycopter...","Facebook"); 


              System.out.println(" value of getactive session "+Session.getActiveSession().getState()+" data "+ data);

              if(Session.getActiveSession().getState().equals("OPENING"))
              {
                  mCurrentSession=Session.openActiveSession(this);  
              }


              Session.getActiveSession().onActivityResult(loginScreen.this, requestCode, resultCode, data);


              mCurrentSession=Session.getActiveSession();

              if(mCurrentSession==null)
              {
                  mCurrentSession=Session.openActiveSession(this);
              } 


              if (mCurrentSession.isOpened()) {

                Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {

                      @Override
                      public void onCompleted(GraphUser user, Response response) {

                            String json=user.getInnerJSONObject().toString();

                            try {
                                JSONObject fbJson=new JSONObject(json);
                                Email=fbJson.getString("email");
                                Name=fbJson.getString("name");
                                Id=fbJson.getString("id");
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            new FbFurther().execute("");

                      }
                    });

                }
            }
Matt
  • 3,882
  • 14
  • 46
  • 89
Prateek
  • 3,923
  • 6
  • 41
  • 79
  • 1
    I had troubles with this method as well. After dozens of hours of debugging, it became obvious that the problem was: The Activity holding this code was set to `noHistory="true"` in the Manifest. After all, I ended up using the official login button that works with Fragments, as the method above is rather dirty, uses internal methods etc. – caw Jul 29 '13 at 11:54

1 Answers1

2

Please DO NOT use SessionTracker. It's in the com.facebook.internal package, and it's not meant for external consumption. It could change or disappear at any time without any guarantees for backwards compatibility. You should be able to do everything you need with just the Session class.

Secondly, it looks like you're trying to do some session management logic inside your onActivityResult method. You should move all of those logic into a Session.StatusCallback, and ONLY pass through the onActivityResult to the session, something like:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Session.getActiveSession().onActivityResult(loginScreen.this, requestCode, resultCode, data);
}
Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • So how the above code for signInWithFacebook would be changed ? Also I have seen #1118578 please contact the app developer toast on my ICS device but this doesn't come with another devices. http://stackoverflow.com/a/13926065/1503130 what would be your comment about this answer ? – Prateek Jul 29 '13 at 07:43
  • Hey @Ming Li, I tried implementing the FB login flow with the UILifeCycleHelper and in certain instances, the `data` param which would normally have a field `mMap`, which contains the FB response, is null. Why would this be happening? – Etienne Lawlor Aug 26 '13 at 07:05