Loader doesn't start after calling initLoader()?
Asked Answered
A

5

16

I have a fragment, and want to start a loader when a button is clicked:

public class MyFragment extends Fragment {

    public void onActivityCreated() {
        super.onActivityCreated();

        Button btn = ...;
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                getLoaderManager().initLoader(500, null, mMyCallback);
            }
        });
    }  

    private LoaderManager.LoaderCallbacks<String> mMyCallback = new  LoaderManager.LoaderCallbacks<String>() {

        @Override
        public Loader<String> onCreateLoader(int arg0, Bundle arg1) {
            Log.e(TAG, "LoaderCallback.onCreateLoader().");
            return new MyLoader(getActivity());
        }
    }
}

public class MyLoader extends AsyncTaskLoader<String> {
    public MyLoader(Context context) {
        super(context);
    }

    @Override
    public String loadInBackground() {
        Log.e(TAG, "Hi, running.");
        return "terrific.";
    }
}

After clicking the button, I can see my callback's onCreateLoader method called, but the created loader never actually starts. Do we need to call forceLoad() on the loader itself to get it to actually start? None of the sample posts do this,

Thanks

Asthenosphere answered 25/4, 2012 at 18:34 Comment(0)
S
32

You need to implement onStartLoading() and call forceLoad() somewhere in the method.

See this post for more information: Implementing Loaders (part 3)

Sikang answered 23/8, 2012 at 16:29 Comment(3)
What should i actually implent in forceLoad() method?Highams
@Manikanta My answer says you should call forceLoad(), not implement it.Sikang
Actually i want to update my ListView continuously from SQLiteOpenHelper, I don't know What way is preferable. Can you please suggest any way?Highams
E
6

In my experience it never worked unless I used forceLoad().

You may find the answer to this previous question helpful: Loaders in Android Honeycomb

Endorsee answered 25/4, 2012 at 19:15 Comment(0)
K
4

Three important points regarding Loaders are:

  1. Always Use forceLoad() method while initialising Loaders. For Example:

    getLoaderManager().initLoader(500, null, mMyCallback).forceLoad();
    
  2. Always implement onStartLoading(). This function will automatically be called by LoaderManager when the associated fragment/activity is being started.

  3. Make sure the ID of the loader is unique otherwise new Loader will not be called.

If there is still a problem you can check the state of loader by calling the isStarted() method.

Khalil answered 7/3, 2015 at 9:54 Comment(0)
B
2

You need to keep a reference to the instance of the loader you create in the method onCreateLoader. Then, to refresh it, call yourLoader.onContentChanged();

Backache answered 1/5, 2012 at 21:8 Comment(1)
OMG, you literally just fixed my entire problem.Nonmetallic
C
2

If you have more than 1 loader in the same activity, make sure their id differs. I lost few hours to figure it out :)

Cuculiform answered 17/10, 2014 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.