title: Using AsyncTask with timer in fragment
body: <p>I'm using Asynctask to load data via Volley. I want to do that AsyncTask invoked after n minutes, maybe this is duplicate and a silly question, but i cant figure how to implement it. Maybe I need to implement AsyncTask in my fragment ?</p>
  
  <p>This is my AsyncTask.class</p>
  
  <pre><code>package com.aa.qc.task;
  
  import android.os.AsyncTask;
  
  import com.aa.qc.callbacks.ZleceniaLoadedListner;
  import com.aa.qc.extras.ZleceniaSorter;
  import com.aa.qc.extras.ZleceniaUtils;
  import com.aa.qc.network.VolleySingleton;
  import com.aa.qc.pojo.Zlecenia;
  import com.android.volley.RequestQueue;
  
  import java.util.ArrayList;
  
  
  public class TaskLoadZlecenia extends AsyncTask&lt;Void, Void, ArrayList&lt;Zlecenia&gt;&gt;{
  
      private ZleceniaLoadedListner myComponent;
      private VolleySingleton volleySingleton;
      private RequestQueue requestQueue;
      private ZleceniaSorter mSorter = new ZleceniaSorter();
  
  
      public TaskLoadZlecenia(ZleceniaLoadedListner myComponent) {
          this.myComponent = myComponent;
          volleySingleton = VolleySingleton.getsInstance();
          requestQueue = volleySingleton.getmRequestQueue();
  
      }
  
  
      @Override
      protected ArrayList&lt;Zlecenia&gt; doInBackground(Void... params) {
  
          ArrayList&lt;Zlecenia&gt; listZlecenias = ZleceniaUtils.loadZlecenia(requestQueue);
          mSorter.sortZleceniaByTime(listZlecenias);
          return listZlecenias;
      }
  
      @Override
      protected void onPostExecute(ArrayList&lt;Zlecenia&gt; listZlecenias) {
          if (myComponent != null) {
              myComponent.onZleceniaLoaded(listZlecenias);
  
          }
      }
  
  }
  </code></pre>
  
  <p>This is listner.class</p>
  
  <pre><code>package com.aa.qc.callbacks;
  
  import com.aa.qc.pojo.Zlecenia;
  
  import java.util.ArrayList;
  
  public interface ZleceniaLoadedListner {
      public void onZleceniaLoaded(ArrayList&lt;Zlecenia&gt; listZlecenias);
  }
  </code></pre>
  
  <p>This is my fragment where i want to repeat asynctask (I have activity with two tabs, each tabs is a fragment)</p>
  
  <pre><code>public class FragmentZlecenia extends Fragment implements SortListener, ZleceniaLoadedListner, SwipeRefreshLayout.OnRefreshListener {
  
      // TODO: Rename parameter arguments, choose names that match
      // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
      private static final String ARG_PARAM1 = "param1";
      private static final String ARG_PARAM2 = "param2";
  
      private ArrayList&lt;Zlecenia&gt; listZlecenias = new ArrayList&lt;&gt;();
      private RecyclerView zleceniaArrived;
      private AdapterZlecenia adapterZlecenia;
  
      private SwipeRefreshLayout mSwipeRefreshLayout;
      private ZleceniaSorter mSorter = new ZleceniaSorter();
  
  
      // TODO: Rename and change types of parameters
      private String mParam1;
      private String mParam2;
  
  
      /**
       * Use this factory method to create a new instance of
       * this fragment using the provided parameters.
       *
       * @param param1 Parameter 1.
       * @param param2 Parameter 2.
       * @return A new instance of fragment FragmentZlecenia.
       */
      // TODO: Rename and change types and number of parameters
      public static FragmentZlecenia newInstance(String param1, String param2) {
          FragmentZlecenia fragment = new FragmentZlecenia();
          Bundle args = new Bundle();
          args.putString(ARG_PARAM1, param1);
          args.putString(ARG_PARAM2, param2);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
      }
  
      public FragmentZlecenia() {
          // Required empty public constructor
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          if (getArguments() != null) {
              mParam1 = getArguments().getString(ARG_PARAM1);
              mParam2 = getArguments().getString(ARG_PARAM2);
          }
      }
  
      public void onSortByTime() {
          L.t(getActivity(), "Sort by time");
          mSorter.sortZleceniaByTime(listZlecenias);
          adapterZlecenia.notifyDataSetChanged();
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
  
          View layout = inflater.inflate(R.layout.fragment_zlecenia, container, false);
  
          mSwipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeZlecenia);
          mSwipeRefreshLayout.setOnRefreshListener(this);
  
  
          zleceniaArrived = (RecyclerView) layout.findViewById(R.id.listZlecenia);
          zleceniaArrived.setLayoutManager(new LinearLayoutManager(getActivity()));
  
          adapterZlecenia = new AdapterZlecenia(getActivity());
          zleceniaArrived.setAdapter(adapterZlecenia);
  
          if (savedInstanceState != null) {
              //if this fragment starts after a rotation or configuration change, load the existing zlecenias from DB
              listZlecenias = MyApplication.getWritableDatabase().getAllZlecenia();
          } else {
              //if this fragment starts for the first time, load the list of zlecenias from a database
              MyApplication.getWritableDatabase().deleteAll();
              //if the database is empty, trigger an AsycnTask to download zlecenias list from the web
              if (listZlecenias.isEmpty()) {
                  new TaskLoadZlecenia(this).execute();
              }
          }
          //update your Adapter to containg the retrieved zlecenias
          adapterZlecenia.setListZlecenia(listZlecenias);
  
          return layout;
      }
  
  
      @Override
      public void onZleceniaLoaded(ArrayList&lt;Zlecenia&gt; listZlecenias) {
  
          L.m("onZleceniaLoaded Fragment");
  
          new TaskLoadZlecenia(this).execute();
          adapterZlecenia.setListZlecenia(listZlecenias);
      }
  
  
      @Override
      public void onRefresh() {
          L.t(getActivity(), "onRefresh");
          new TaskLoadZlecenia(this).execute();
  
      }
  
  }
  </code></pre>