简体   繁体   中英

onCreateLoader() only called one time

I've a fragment that extends ListFragment and implements LoaderManager.LoaderCallbacks. Inside this fragment i have a button that should call a webservice and fill the List.

The problem is this method that only is called once, only when the fragment is created, and it would me be called every time that i press the button:

@Override
public Loader<List<ResultSearchUser>> onCreateLoader(int arg0, Bundle arg1) {
    return new SearchUsersTask(getActivity(), page, size);
}

Here is my button:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
   ...
    Button buttonSearchUsers = (Button) mLinearLayout
            .findViewById(R.id.buttonSearchUsers);
    buttonSearchUsers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (user.getInterests() != null
                    && user.getInterests().size() > 0) {
                page++;
                getLoaderManager().initLoader(0, null, SearchFragment.this);
            } 
        }
    });
   ....

Why onCreatedLoader() is only called when the fragment is created? And why the method onLoadFinished() is called everytime that i press the button? What is the correct way to call my Task and fill the List using the method onLoadFinished()?

Thank in advance.

From the documentation :

Ensures a loader is initialized and active. If the loader doesn't already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used.

In other words, initLoader() will only trigger a call to onCreateLoader() when the Loader isn't already created. On subsequent calls, the Loader will be reused.

If you need to forcefully destroy/create an existing Loader , you should call LoaderManager#restartLoader() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM