Back to project page SeeKampf.
The source code is released under:
GNU General Public License
If you think the Android project SeeKampf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.avedo.seekampf.fragments; // w w w. j a v a2 s . c o m import java.lang.reflect.ParameterizedType; import net.avedo.seekampf.R; import net.avedo.seekampf.core.VolleyActivity; import net.avedo.seekampf.utils.AuthGsonRequest; import net.avedo.seekampf.utils.Constants; import net.avedo.seekampf.utils.VolleyErrorHelper; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import android.app.Fragment; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; public abstract class RestDetailsFragment<ServiceObject extends Object> extends Fragment { protected SharedPreferences settings; protected Resources res; protected AuthGsonRequest<ServiceObject> request; protected boolean isFetching = false; protected int retryCount = 0; protected ServiceObject serviceObject; protected long serviceObjectId; @Override public void onActivityCreated(Bundle paramBundle) { // Fetch the application preferences handle ... this.settings = PreferenceManager.getDefaultSharedPreferences(this.getActivity()); // ... and the application resources. this.res = getResources(); // Get the id of the service object. Bundle extras = getArguments(); if (extras == null || ((serviceObjectId = extras.getLong(Constants.INTENT_EXTRA_ID, Constants.DEFAULT_ID)) == Constants.DEFAULT_ID)) { finish(Constants.ACTIVITY_RESULT_MISSING_ARGUMENT); } super.onActivityCreated(paramBundle); } @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); } @Override public void onStart() { super.onStart(); fetchData(); } @Override public void onDestroyView() { VolleyActivity.getInstance().cancelPendingRequests(fetchServiceTag()); isFetching = false; super.onDestroyView(); } protected void finish(int resultCode) { getActivity().setResult(resultCode); getActivity().finish(); } protected void fetchData() { // Break if already fetching data or there is nothing left to load. if (isFetching) { return; } // Start fetching data ... isFetching = true; // ... and generate the service url. String serviceURL = fetchServiceUrl(); // Setup the request object. request = new AuthGsonRequest<ServiceObject>(Request.Method.GET, serviceURL, fetchServiceObjClass(), null, new Response.Listener<ServiceObject>() { @Override public void onResponse(ServiceObject serviceObject) { if (RestDetailsFragment.this.getView() != null && serviceObject != null) { // Reset the retry counter, ... retryCount = 0; // ... store the current object, ... RestDetailsFragment.this.serviceObject = serviceObject; // ... setup the main layout, ... RestDetailsFragment.this.createView(); // ... update offset and reset fetching status. isFetching = false; } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { retryCount++; if (retryCount < Constants.MAXIMUM_RETRIES) { VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag()); } Toast.makeText(getActivity(), VolleyErrorHelper.getMessage(error, getActivity()), Toast.LENGTH_LONG).show(); } }); VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag()); } protected Class<ServiceObject> fetchServiceObjClass() { ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); return (Class<ServiceObject>) parameterizedType.getActualTypeArguments()[0]; } protected abstract String fetchServiceUrl(); protected abstract String fetchServiceTag(); protected abstract void createView(); }