title: Android getActivity() always returns null inside fragment
body: <p>I have a strange problem in which I encountered:The getActivity() method <strong>always</strong> returns null inside fragment. I calling it after the onAttach() and onCreateView() are  finishing their run.</p>
  
  <p>This fragment lives inside a FragmentActivity() that contains stack of fragments, and the way I adding fragment to it is:</p>
  
  <p>(This code is being called from the onCreate() of the Fragment Activity())</p>
  
  <pre><code>SmartFragment fragment;
  fragment = (SmartFragment) Fragment.instantiate(this,
  fragmentClassName, params);     
  mStackOfFragments.add(fragment);
  FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
  trans.add(R.id.tabcontent, fragment);
  trans.addToBackStack(null);
  trans.commitAllowingStateLoss();
  </code></pre>
  
  <p>I hope it is clear enough</p>
  
  <p>Edit 1:</p>
  
  <p>The call to getActivity():</p>
  
  <pre><code>protected OnDoneListener nDoneListener = new OnDoneListener() {
  
      @Override
      public void OnDone(final int counter, final String name) {
  
  
          if (getActivity() != null)
              ((TabActivity) getActivity()).RunOnUiThread(new Runnable() {
  </code></pre>
  
  <p>...
     ...</p>
  
  <p>This callback is being called from a different class.</p>
  
  <p>Edit 2:</p>
  
  <pre><code>class MemoryManager()
  {
  
      private OnDoneListener nDoneListener;
  
      public void setOnDoneListener(OnDoneListener onDoneListener)
      {
      this.onDoneListner = onDoneListener;
      }
  
      public void updateUiOnRequestFinish()
      {
        onDoneListener.onDone();
      }
  
  }
  </code></pre>
  
  <p>The MemoryManaget itself calls to updateUiOnRequestFinish() from a different callback</p>
  
  <p>Edit 3:</p>
  
  <p>The FragmentManager logs are:</p>
  
  <pre><code>04-08 18:44:05.950: V/FragmentManager(16280): Commit: BackStackEntry{41f9bd60}
  `04-08 18:44:05.950: D/FragmentManager(16280):   mName=null mIndex=-1 mCommitted=false
  04-08 18:44:05.950: D/FragmentManager(16280):   Operations:
  04-08 18:44:05.950: D/FragmentManager(16280):     Op #0: ADD FragmentMyProfile{41f9bc20 id=0x7f070126}
  04-08 18:44:05.950: V/FragmentManager(16280): Setting back stack index 0 to BackStackEntry{41f9bd60}
  04-08 18:44:05.950: V/FragmentManager(16280): Run: BackStackEntry{41f9bd60 #0}
  04-08 18:44:05.950: V/FragmentManager(16280): Bump nesting in BackStackEntry{41f9bd60 #0} by 1
  04-08 18:44:05.950: V/FragmentManager(16280): Bump nesting of FragmentMyProfile{41f9bc20 id=0x7f070126} to 1
  04-08 18:44:05.950: V/FragmentManager(16280): add: FragmentMyProfile{41f9bc20 id=0x7f070126}
  04-08 18:44:05.950: V/FragmentManager(16280): Allocated fragment index FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
  04-08 18:44:05.950: V/FragmentManager(16280): moveto CREATED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
  04-08 18:44:05.950: V/FragmentManager(16280): moveto ACTIVITY_CREATED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
  04-08 18:44:05.990: V/FragmentManager(16280): moveto STARTED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
  04-08 18:44:06.030: V/FragmentManager(16280): moveto RESUMED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
  `
  </code></pre>
  
  <p>And it looks fine to me. So I'm guessing that this bug relates to the way I call the getActivity() from the callback.</p>