Created
April 4, 2014 08:16
-
-
Save FireZenk/9970302 to your computer and use it in GitHub Desktop.
Android(fragment) abstract class that contains a background thread to handle network connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.firezenk.util; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.actionbarsherlock.app.SherlockFragment; | |
/** | |
* NetworkFragment Class | |
* This class extends SherlockFragment and contains one AsyncTask | |
* to do any required network job on background thread | |
* @author jorgegarridooval | |
* @date 01/04/2014 | |
*/ | |
public abstract class NetworkFragment extends SherlockFragment { | |
protected final String LOG_TAG = "LOG_ME"; | |
protected final boolean DEBUG_MODE = false; | |
private RequireData requireData; | |
@Override | |
public void onStart() { | |
super.onStart(); | |
Log.d(LOG_TAG, "On start "+getClass().toString()); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
Log.d(LOG_TAG, "On stop "+getClass().toString()); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
Log.d(LOG_TAG, "On pause "+getClass().toString()); | |
if(requireData!=null) | |
requireData.cancel(true); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
Log.d(LOG_TAG, "On resume "+getClass().toString()); | |
} | |
protected class RequireData extends AsyncTask<Object, Object, Object> { | |
@Override | |
protected Object doInBackground(Object... params) { | |
Log.d(LOG_TAG, "Require data: do in background "+getClass().toString()); | |
return requireDoInBackground(params); | |
} | |
@Override | |
protected void onPostExecute(Object result) { | |
super.onPostExecute(result); | |
Log.d(LOG_TAG, "Require data: on post execute "+getClass().toString()); | |
requireOnPostExecute(result); | |
} | |
} | |
protected abstract Object requireDoInBackground(Object... param); | |
protected abstract Object requireOnPostExecute(Object result); | |
public RequireData getRequireData() { | |
return requireData = new RequireData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment