title: Facebook Android SDK: why is the Activity recreated before calling Fragment's onActivityResult?
body: <p>I wrote a <code>Fragment</code> to manage Facebook log in/out with Facebook Android SDK 3.5.2, following the example given <a href="https://developers.facebook.com/docs/android/login-with-facebook/" rel="nofollow">here</a>.</p>
  
  <p>When the login process through Facebook web widgets is done, <code>MainActivity</code> is started again (<code>onCreate</code> is called). </p>
  
  <p>Then in the example code if <code>savedInstanceState!=null</code> in <code>MainActivity</code>'s <code>onCreate</code>, the <code>savedInstanceState</code> is checked to know if the existing <code>FacebookPrefsFragment</code> instance must be restored or if a new instance must be created:</p>
  
  <pre><code>if (savedInstanceState == null) {
      // Add the fragment on initial activity setup
      mainFragment = new MainFragment();
      getSupportFragmentManager()
      .beginTransaction()
      .add(android.R.id.content, mainFragment)
      .commit();
  } else {
      // Or set the fragment from restored state info
      mainFragment = (MainFragment) getSupportFragmentManager()
      .findFragmentById(android.R.id.content);
  }
  </code></pre>
  
  <p>My problem is that my <code>MainActivity</code> has many <code>Fragments</code>, and <code>savedInstanceState</code> can be not null not only when on the Facebook login process. So I have two questions:</p>
  
  <ul>
  <li><p>Why is <code>MainActivity</code> created again after the Facebook login process? Shouldn't just <code>FacebookPrefsFragment</code>'s <code>onActivityResult</code> be called?</p></li>
  <li><p>If it's going to be created, how can I know that it is because the Facebook SDK called it?</p></li>
  </ul>
  
  <p><b>MainActivity</b></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          if (savedInstanceState != null) {       
  
              // If the Activity has been started after the Facebook login process, restart the FacebookPrefsFragment,
              // but how can I be sure that MainActivity has been called by the Facebook app?
  
              FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
              Fragment facebookPrefsFragment = new FacebookPrefsFragment();       
              fragmentTransaction.replace(R.id.content_container, facebookPrefsFragment, "facebookPrefsFragment");
              fragmentTransaction.commit();
          }
          else{
              // start other Fragment
          }
      }
  
      // ...
  }
  </code></pre>
  
  <p><b>FacebookPrefsFragment</b></p>
  
  <pre><code>public class FacebookPrefsFragment extends Fragment{
  
      private void onSessionStateChange(Session session, SessionState state, Exception exception) {
          if (state.isOpened()) {
              Log.i(this.getClass().getSimpleName(), "Logged in", MyApplication.LOG_LEVEL);
          } else if (state.isClosed()) {
              Log.i(this.getClass().getSimpleName(), "Logged out", MyApplication.LOG_LEVEL);
          }
      }
  
      private Session.StatusCallback callback = new Session.StatusCallback() {
          @Override
          public void call(Session session, SessionState state, Exception exception) {
              onSessionStateChange(session, state, exception);
          }
      };
  
      private UiLifecycleHelper uiHelper;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.facebook_prefs, container, false);
  
          LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
          authButton.setPublishPermissions(Arrays.asList("publish_actions"));
          authButton.setFragment(this);
  
          return view;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          uiHelper = new UiLifecycleHelper(getActivity(), callback);
          uiHelper.onCreate(savedInstanceState);
      }
  
      @Override
      public void onResume() {
          super.onResume();
  
          Session session = Session.getActiveSession();
          if (session != null &amp;&amp;
                 (session.isOpened() || session.isClosed()) ) {
              onSessionStateChange(session, session.getState(), null);
          }
  
          uiHelper.onResume();
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          uiHelper.onActivityResult(requestCode, resultCode, data);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          uiHelper.onPause();
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          uiHelper.onDestroy();
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          uiHelper.onSaveInstanceState(outState);
      }
  }
  </code></pre>