package net.appz.feedloader;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

/**
 * Created by App-z.net on 04.04.15.
 */
public class FeedLoader {

    private static volatile FeedLoader singleton = null;

    private Context context = null;
    private boolean DEBUG = true;
    private String TAG = getClass().getSimpleName();

    private DispatcherData dispatcherData = new DispatcherData();

    public FeedLoader(Context context) {
        this.context = context;
    }

    LoaderManager.LoaderCallbacks<Object> callback = new LoaderManager.LoaderCallbacks<Object>() {
        @Override
        public Loader<Object> onCreateLoader(int id, Bundle args) {
            return new FeedLoaderWrapper(context , args);
        }

        @Override
        public void onLoadFinished(Loader<Object> loader, Object data) {
            dispatcherData.callBackMap.get(loader.getId()).onResponse(loader.getId(), data);
        }

        @Override
        public void onLoaderReset(Loader<Object> loader) {

        }
    };

    public FeedLoader addLoader(int loaderId,
                                String url,
                                Class<?> loaderClazz,
                                DispatcherData.Listener callback){
        dispatcherData.loaderClazzMap.put(loaderId, loaderClazz);
        dispatcherData.callBackMap.put(loaderId, callback);
        dispatcherData.urlFeeds.put(loaderId, url);
        return singleton;
    }

    public void start(int loaderId, final Context context) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("dispatcherData", dispatcherData);

        ((FragmentActivity)context).getSupportLoaderManager().
                restartLoader(loaderId, bundle, callback);
    }

    public void stop(int loaderId, final Context context) {
        ((FragmentActivity)context).getSupportLoaderManager().destroyLoader(loaderId);
    }

    public static FeedLoader with(Context context) {
        if (singleton == null) {
            synchronized (FeedLoader.class) {
                if (singleton == null) {
                    singleton = new FeedLoader(context);
                }
            }
        }
        return singleton;
    }
}

@@ -609,16 +609,40 @@
  context
+.getApplicationContext()
 ;%0A    %7D%0A
@@ -1643,39 +1643,48 @@
 loaderId, final 
-Context
+FragmentActivity
  context) %7B%0A    
@@ -1781,25 +1781,24 @@
 rData);%0A
-%0A
         
 ((Fragme
@@ -1781,34 +1781,101 @@
 rData);%0A        
-((
+assert context instanceof FragmentActivity : %22Run possible only from 
 FragmentActivity
@@ -1870,33 +1870,42 @@
 mentActivity
-)
+%22;%0A        
 context
-)
 .getSupportL

Fix according comments http://habrahabr.ru/post/254989/