lazexe
lazexe

Reputation: 369

AsyncTaskLoader not starting

I have some trouble with loaders but I don't understand what I do wrong. This is fragment that init and start Loader

import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pixelark.bulletinplus.R;
import com.pixelark.bulletinplus.async.ChurchListLoader;
import com.pixelark.bulletinplus.debug.Debug;
import com.pixelark.bulletinplus.model.ChurchListItem;

import java.util.ArrayList;

    public class ChurchListFragment extends android.support.v4.app.Fragment
            implements LoaderManager.LoaderCallbacks<ArrayList<ChurchListItem>> {

    private static final int CHURCH_LIST_LOADER_ID = 777;

    public ChurchListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_church_list, container, false);
        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setTitle("ChurchListFragment");
        getLoaderManager().initLoader(CHURCH_LIST_LOADER_ID, null, this);
        return rootView;
    }

    @Override
    public Loader<ArrayList<ChurchListItem>> onCreateLoader(int id, Bundle args) {
        Debug.d(Debug.LOADER_DEBUG_TAG, "onCreateLoader");
        return new ChurchListLoader(getContext());
    }

    @Override
    public void onLoadFinished(Loader<ArrayList<ChurchListItem>> loader, ArrayList<ChurchListItem> data) {
        Debug.d(Debug.LOADER_DEBUG_TAG, "onLoadFinished");
    }

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

    }
}

This is a simple loader that I write:

import android.content.Context;
import android.util.Log;

import com.pixelark.bulletinplus.contract.UrlContract;
import com.pixelark.bulletinplus.debug.Debug;
import com.pixelark.bulletinplus.model.ChurchListItem;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class ChurchListLoader extends android.support.v4.content.AsyncTaskLoader<ArrayList<ChurchListItem>> {

    public ChurchListLoader(Context context) {
        super(context);
    }

    @Override
    public ArrayList<ChurchListItem> loadInBackground() {
        Debug.d(Debug.LOADER_DEBUG_TAG, "loadInBackground");
        try {
            URL url = new URL(UrlContract.URL_BASE + UrlContract.URL_BASE);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            String responseMessage = connection.getResponseMessage();
            InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                Log.d(Debug.LOADER_DEBUG_TAG, "Line: " + line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Pay your attention on the LOGS.

The log output: D/LOADER_DEBUG_TAG: onCreateLoader

That's all log output, I don't understand why Loader not start method loadInBackground();

Thanks.

Upvotes: 1

Views: 499

Answers (2)

tiny sunlight
tiny sunlight

Reputation: 6251

Yout should call it youself.

   @Override
    public Loader<ArrayList<ChurchListItem>> onCreateLoader(int id, Bundle args) {
        Debug.d(Debug.LOADER_DEBUG_TAG, "onCreateLoader");
        return new ChurchListLoader(getContext()).startLoading();
    }

Upvotes: 0

kris larson
kris larson

Reputation: 30985

I was a bit astonished to find that AsyncTaskLoader doesn't automatically start the same way that AsyncTask does.

Here is a minimal example of an override for onStartLoading() that will get you going:

   @Override
   protected void onStartLoading() {
       forceLoad();
   }

You may need to add some logic to this method depending on your needs. This will always try to get a new data set. If you have already loaded the data and it hasn't changed, you can call deliverResult() right away.

Take a look at the JavaDocs for AsyncTaskLoader to see an example implementation of onStartLoading().

Upvotes: 5

Related Questions