title: java.lang.IllegalStateException: Fragment not attached to a context
body: <p>I have a tablayout with a viewpager in my MainActivity. </p>
  
  <p>My <code>PagerAdapter</code> looks like this:</p>
  
  <pre><code>public class MainActivityPagerAdapter extends PagerAdapter {
  
      public MainActivityPagerAdapter(FragmentManager fm, int numOfTabs) {
          super(fm, numOfTabs);
      }
  
      @Override
      public Fragment getItem(int position) {
  
          switch (position) {
              case 0:
                  return new StoresFragment();
              case 1:
                  return new OrdersFragment();
              default:
                  return null;
          }
      }
  }
  </code></pre>
  
  <p>I am coming back from another activity like this:</p>
  
  <pre><code>Intent intent = new Intent(context, MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
  finish(); //finishAffinity();
  </code></pre>
  
  <p>But then I get an <code>java.lang.IllegalStateException</code> in one of my Fragments in the viewpager of the MainActivity.</p>
  
  <p>I read many related questions and tried to solve this. It is said, that this happens when one keeps references to Fragments outside of the PagerAdapter. But I am not doing this, as you can see in my code.</p>
  
  <p>Does anyone know what I am doing wrong?</p>
  
  <p><strong>Edit - Stacktrace</strong></p>
  
  <pre><code>FATAL EXCEPTION: main
      Process: com.lifo.skipandgo, PID: 23665
      java.lang.IllegalStateException: Fragment OrdersFragment{42c2a740} not attached to a context.
      at android.support.v4.app.Fragment.requireContext(Fragment.java:614)
      at android.support.v4.app.Fragment.getResources(Fragment.java:678)
      at com.lifo.skipandgo.activities.fragments.OrdersFragment$1.results(OrdersFragment.java:111)
      at com.lifo.skipandgo.connectors.firestore.QueryResult.receivedResult(QueryResult.java:37)
      at com.lifo.skipandgo.controllers.UserController$2.onUpdate(UserController.java:88)
      at com.lifo.skipandgo.connectors.firestore.QuerySubscription.onEvent(QuerySubscription.java:59)
      at com.lifo.skipandgo.connectors.firestore.QuerySubscription.onEvent(QuerySubscription.java:18)
      at com.google.firebase.firestore.zzg.onEvent(Unknown Source)
      at com.google.firebase.firestore.g.zzh.zza(SourceFile:28)
      at com.google.firebase.firestore.g.zzi.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:733)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:146)
      at android.app.ActivityThread.main(ActivityThread.java:5653)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>Edit:</strong>
  Interesting is, that the view has defenitely loaded when the error occurs. Because the error occurs about 10-15 seconds later after the fragment is shown again. I this in my orderFragment, where the error occurs:</p>
  
  <pre><code>orders = new QueryResult&lt;UserOrder&gt;(UserOrder.class) {
              @Override
              public void results(List&lt;UserOrder&gt; results) { 
  orderLoadingMessage.setBackgroundColor(getResources().getColor(R.color.green)); 
      }
  }
  </code></pre>
  
  <p>I do this in <code>onCreateView</code> and this result comes about 10-15 seconds after the view loaded.</p>