title: Android Facebook onActivityResult data returning null
body: <p>I am a complete newbee to Android development and trying to integrate Facebook Login. Following the getting started I have successfully integrated the Facebook login button. The Facebook login button changes to logout. Then logout to login as intended.</p>
  
  <p>Now the problem is at onActivityResult the data I am receiving is null. I have spent hours and searched many Stackoverflow links but none I could understand or find any answer. I am sure I am going wrong somewhere, but I just can't seem to figure out where. Any help will be greatly appreciated.</p>
  
  <p>When I try data.GetDataString, I get null.</p>
  
  <p>When I try </p>
  
  <pre><code> if(resultCode == RESULT_OK){
          Bundle bundle = data.getExtras();
  
          String id = bundle.toString();
          System.out.println("I am inside resultcode " +id);
      }else
      {
          System.out.println("Not okay");
      }
  </code></pre>
  
  <p>In the console it prints</p>
  
  <pre><code>Data is Bundle[{com.facebook.LoginActivity:Result=com.facebook.AuthorizationClient$Result@42a54038}]
  </code></pre>
  
  <p>I then tried to print individual keys using the following</p>
  
  <pre><code>for (String key: bundle.keySet())
  {
    Log.d ("myApplication", key + " is a key in the bundle");
  }
  </code></pre>
  
  <p>Then I got this in the console. </p>
  
  <pre><code>com.facebook.LoginActivity:Result is a key in the bundle
  </code></pre>
  
  <p>Now my Facebook code is.</p>
  
  <pre><code>public class SignupActivity extends ActionBarActivity{
  
  private static final String TAG = "Custom debug";
  private UiLifecycleHelper uiHelper;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      try {
          PackageInfo info = getPackageManager().getPackageInfo(
                  "com.test.testApp",
                  PackageManager.GET_SIGNATURES);
          for (Signature signature : info.signatures) {
              MessageDigest md = MessageDigest.getInstance("SHA");
              md.update(signature.toByteArray());
              Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
          }
      } catch (PackageManager.NameNotFoundException e) {
  
      } catch (NoSuchAlgorithmException e) {
  
      }
  
      setContentView(R.layout.activity_signup);
  
      LoginButton fbAuthBtn = (LoginButton) findViewById(R.id.fbSignupBtn);
      fbAuthBtn.setReadPermissions(Arrays.asList("public_profile","email"));
  
      uiHelper = new UiLifecycleHelper(this, callback);
      uiHelper.onCreate(savedInstanceState);
  
  
  
  }
  
  @Override
  protected void onResume() {
      super.onResume();
  
      // Logs 'install' and 'app activate' App Events.
      AppEventsLogger.activateApp(this);
  }
  
  @Override
  protected void onPause() {
      super.onPause();
  
      // Logs 'app deactivate' App Event.
      AppEventsLogger.deactivateApp(this);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      uiHelper.onDestroy();
  }
  
  
  //Facebook methods
  private void onSessionStateChange(Session session, SessionState state, Exception exception) {
      if (state.isOpened()) {
          Log.i(TAG, "Logged in...");
      } else if (state.isClosed()) {
          Log.i(TAG, "Logged out...");
      }
  }
  
  private Session.StatusCallback callback = new Session.StatusCallback() {
      @Override
      public void call(Session session, SessionState state, Exception exception) {
          onSessionStateChange(session, state, exception);
      }
  };
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
     uiHelper.onActivityResult(requestCode, resultCode, data);
      //Session.getActiveSession().onActivityResult(SignupActivity.this, requestCode, resultCode, data);
  
  
  
      Log.i(TAG, "Request code is " + requestCode + " ResultCode is " + resultCode + " And the data is " + data.getDataString());
  
      if(resultCode == RESULT_OK){
          Bundle bundle = data.getExtras();
  
          String fbData = bundle.toString();
          System.out.println("I am inside resultcode " +fbData);
      }else
      {
          System.out.println("I have no idea what is happening :(");
      }
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      uiHelper.onSaveInstanceState(outState);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thank you very much in advance. Please let me know if you guys require any additional info.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have finally figured out the solution. So I am adding the solution here so that it helps some one else like me who ends up searching for hours or save some precious hours. Well in the above code I was trying to print the data in onActivityResult. But that is not correct.</p>
  
  <pre><code>uiHelper.onActivityResult(requestCode, resultCode, data);
  </code></pre>
  
  <p>The above code calls </p>
  
  <pre><code> private void onSessionStateChange(Session session, SessionState state, Exception exception) {
      if (state.isOpened()) {
          Log.i(TAG, "Logged in...");
      } else if (state.isClosed()) {
          Log.i(TAG, "Logged out...");
      }
  }
  </code></pre>
  
  <p>When the onSessionSateChange is called modify the above to the following</p>
  
  <pre><code>private void onSessionStateChange(Session session, SessionState sessionState, Exception exception) {
      if (sessionState.isOpened()) {
          Log.i(TAG, "Logged in...");
  
          Request.newMeRequest(session, new Request.GraphUserCallback() {
              @Override
              public void onCompleted(GraphUser user, Response response) {
  
                  if(user != null){
                      String firstname = user.getFirstName();
                      String lastname = user.getLastName();
                      String id = user.getId();
                      String email = user.getProperty("email").toString();
                      String dob = user.getBirthday();
                      String username = user.getUsername();
  
                      Log.i(TAG, "id is " + id + " First name is " + firstname + " Lastname is " + lastname + " and email is " + email );
  
                  }else
                  {
                      Log.i(TAG, "Screwed :( ");
                  }
              }
          }).executeAsync();
  
      } else if (sessionState.isClosed()) {
          Log.i(TAG, "Logged out...");
      }
  }
  </code></pre>
  
  <p>so from the above, if the session is opened, Facebook's graph API is called which returns the data on a callback to onCompleted method of newMeRequest. This is where we finally get access to the data directly.
  And I believe inside onCompleted method we can add our code to proceed to further activities. </p>
  
  <p>Upon further research, the typed methods can be retrieved directly like username, id, first name etc. The non typed methods like email, locale etc should be retrieved using the getProperty method.</p>
  
  <p>So finally the above code produces the following result on console.
  id is 123456 First name is Blahfirst Lastname is Blahlast and email is blah@gmail.com</p>
  
  <p>Here is the link to Facebook's graph api calls and complete info on how to retrieve data.
  <a href="https://developers.facebook.com/docs/android/graph#userdata-step1" rel="nofollow">https://developers.facebook.com/docs/android/graph#userdata-step1</a></p>
  
  <p>Hope the solution helps someone. I am off to further development now :)</p>