hqt

HeadlessFragment - AsyncTask

hqt
Dec 26th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.81 KB | None | 0 0
  1. package com.hqt.hac.helper.task;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8.  
  9. import static com.hqt.hac.utils.LogUtils.makeLogTag;
  10.  
  11. /**
  12.  * Use this fragment for running background task using AsyncTask
  13.  * because Android retains Fragment (different from Activity). so itself will be survive across configuration changes
  14.  * we will use this trick to fix configuration change when using AsyncTask
  15.  * *Note* because Java does not support real Callback method. we create one for convenience
  16.  *
  17.  * Created by ThaoHQSE60963 on 12/26/13.
  18.  */
  19. public class HeadlessFragment extends Fragment {
  20.     /** tag to recognize fragment signature */
  21.     public static String TAG = makeLogTag(HeadlessFragment.class);
  22.  
  23.     /** AsyncTask for running background */
  24.     public RotationAsyncTask task;
  25.  
  26.     /** callback method for running AsyncTask
  27.      * in fact. it's our activity
  28.      */
  29.     public ITaskCallback mCallback;
  30.  
  31.     /** boolean variable to know is this work finish or not */
  32.     private boolean isFinish = false;
  33.  
  34.     /** this method should be only called at first created */
  35.     @Override
  36.     public void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.  
  39.         //The heart and mind of headless fragment
  40.         // It will keep the fragment alive during configuration change of activity
  41.         setRetainInstance(true);
  42.  
  43.         // running long action
  44.         task = new RotationAsyncTask();
  45.         task.execute();
  46.     }
  47.  
  48.     /** when configuration changes. attach again activity
  49.      * this fragment just only is called when retained
  50.      */
  51.     @Override
  52.     public void onAttach(Activity activity) {
  53.         super.onAttach(activity);
  54.         mCallback = (ITaskCallback) activity;
  55.         ((AsyncActivity) activity).dialog = new ProgressDialog(activity);
  56.     }
  57.  
  58.     /** when configuration changes. old activity is lost
  59.      * assign Callback to null make Garbage Collector collects old activity
  60.      */
  61.     @Override
  62.     public void onDetach() {
  63.         super.onDetach();
  64.         mCallback = null;
  65.     }
  66.  
  67.     /**
  68.      * Activity call this class to publish progress to UI
  69.      */
  70.     public void publishProgressToUI(int progress) {
  71.         task.publishProgressToUI(progress);
  72.     }
  73.  
  74.     /** if this work is finish. can set this work to null for another work */
  75.     public boolean isFinish() {
  76.         return  isFinish;
  77.     }
  78.  
  79.     /** make this AsyncTask Private Inner to access Callback without get/set again */
  80.     private class RotationAsyncTask extends AsyncTask<Void, Integer, Integer> {
  81.  
  82.         @Override
  83.         protected void onPreExecute() {
  84.             super.onPreExecute();
  85.             if (mCallback != null) mCallback.onPreExecute();
  86.         }
  87.  
  88.         @Override
  89.         protected void onPostExecute(Integer status) {
  90.             super.onPostExecute(status);
  91.             isFinish = true;
  92.             if (mCallback != null) mCallback.onPostExecute(status);
  93.         }
  94.  
  95.         @Override
  96.         protected void onProgressUpdate(Integer... values) {
  97.             super.onProgressUpdate(values);
  98.             if (mCallback != null) mCallback.onProgressUpdate(values);
  99.         }
  100.  
  101.         @Override
  102.         protected void onCancelled() {
  103.             super.onCancelled();
  104.             if (mCallback != null) mCallback.onCancel();
  105.         }
  106.  
  107.         @Override
  108.         protected Integer doInBackground(Void... params) {
  109.             if (mCallback != null) return mCallback.doInBackground();
  110.             else return -1;
  111.         }
  112.  
  113.         /** because publishProgress is protected. use this method to public to outside */
  114.         public void publishProgressToUI(Integer progress) {
  115.             publishProgress(progress);
  116.  
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment