title: Scheduled Task in Fragment returns getActivity as null
body: <p>I know similar type of question is asked before. Sorry to ask again.<br/>
  I have a fragment in a tab FragmentActivity. Within the fragment in <b>onActivityCreated</b>, I have to schedule a task after every fix interval.</p>
  
  <pre><code>Timer t = new Timer();
  t.scheduleAtFixedRate(new TimerTask() {
  @Override
  public void run() {
     getActivity().runOnUiThread(new Runnable() {
              @Override
      public void run() {
          new fetchDataInBackground(ctx).execute();           //async task
      }
     });
  }
  }, 0, 20000);
  </code></pre>
  
  <p>The above code works perfectly fine, until we press the back button over the <b>tab activity</b>.<br/>
  Once back button is pressed it throws the null pointer exception on getActivity() of above code.<br/>
  I guess may be fragment have been detached from the activity therefore getActivity() is returning null.
  My question is how to achieve above scenario, so that the process should continue even if the back button is pressed. Any best practices? <br/>
  I am returning the same instance of Fragment in getItem method from the FragmentPageAdapter.<br/></p>
  
  <p>Thanks in advance!</p>