title: NullPointerException accessing activity from PreferenceFragment
body: <p>Edit: Changing the language results in SettingsActivity.onCreate being called twice: (1) Due to recreate() and (2) due to context change.
  However SettingsFragment.onCreate is being called only once, which kind of explains below question. But why isnt't SettingsFragment.onCreate being called after the second execution of the SettingsActivity.onCreate?</p>
  
  <hr>
  
  <p>I am quite new to Android development and want to understand the reason which makes my application crash:</p>
  
  <p>Within a settings activity I have placed a PreferenceFragment which allows the user to change the UI language. In order to take effect and not to spoil the back stack I call the recreate() method of the fragment's activity.
  The first implementation looked like below: </p>
  
  <p>(A)</p>
  
  <pre><code>public class SettingsActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          SettingsFragment fragment = new SettingsFragment();
          getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
          getFragmentManager().executePendingTransactions();
          fragment.setActivity(this);
      }
  
  
      public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
      {
  
          Activity m_activity;
  
          public void setActivity(Activity activity){
              m_activity = activity;
          }
  
          @Override
          public void onCreate(final Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              addPreferencesFromResource(R.xml.preferences);
              SharedPreferences preferences = getPreferenceScreen().getSharedPreferences();
              preferences.registerOnSharedPreferenceChangeListener(this);
          }
  
  
          @Override
          public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                  ...
                  m_activity.recreate();
          }
      }
  }
  </code></pre>
  
  <p>Sometimes above solution works, sometimes m_activity is Null and the application crahes. As a possible alternative solution I removed the 'setActivity' setter and called 'getActivity' within onSharedPreferenceChanged:</p>
  
  <p>(B)</p>
  
  <pre><code> @Override
   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                  ...
                  getActivity().recreate();
    }
  </code></pre>
  
  <p>But this also results in sporadic NullPointerExceptions. </p>
  
  <p>In scenario (B) I found that the fragment's 'onAttach' is not being called for any reason sometimes and hence 'getActivity' returns null. However I would expect scenario (A) not to crash since <strong>this</strong> isn't Null in onCreate?</p>