title: getActivity() returns null in ActionBar Fragment
body: <p><code>getActivity()</code> within a Fragment will return <code>null</code> if called before <code>fragment.onAttach(activity)</code>. </p>
  
  <p><code>fragment.onActivityCreated(savedInstanceState)</code> is called after <code>fragment.onAttach(activity)</code>. See <a href="http://developer.android.com/reference/android/app/Fragment.html#Lifecycle" rel="nofollow">developer.android</a> for info on fragment lifecycyle.</p>
  
  <p>I have a method within my fragment that attempts to instantiate a database connection. When the method initially is called, it works fine. </p>
  
  <pre><code>public void onActivityCreated(Bundle savedInstanceState){
     super.onActivityCreated(savedInstanceState);
      Log.d("MyTag", "Activity being created");
      if (savedInstanceState == null){
          listAdapter.setContext(getActivity());
          execSearch(0);
      } else { listAdapter.setContext(getActivity()); }
  }
  
  public void execSearch(Long searchId) {
      MySQLiteHelper dbHelper = new MySQLiteHelper(getActivity());
      SQLiteDatabase database = dbHelper.getReadableDatabase();
      ... 
  }
  </code></pre>
  
  <p>Now when this same method is called via an interface after a button click within a listview, <code>getActivity()</code> returns <code>null</code> (in the fragment.</p>
  
  <p>Here is the button callback from within the ListAdapter:</p>
  
  <pre><code>private final View.OnClickListener editSearchListener = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Long search_id = (Long) v.getTag();
          ((MyInterface) context).searchCalled(search_id);
      }
  };
  </code></pre>
  
  <p>And the interface implementation within the MainActivity:</p>
  
  <pre><code>@Override
  public void searchCalled(Long search_id) {
      fragment.execSearch(id);
  }
  </code></pre>
  
  <p>This second time around I get the <code>null</code> response from <code>getActivity()</code> within <code>fragment.execSearch(id)</code>.</p>
  
  <p>I'm stuck!! Does this have something to do with the interface call coming from a UI action. Do I need to implement mainActivity.searchCalled as a runnable on the UI thread???</p>