javaandroidandroid-studioandroid-loadermanager

loadermanager.loadercallbacks android studio


I am having trouble with LoaderCallbacks in my project. I have implements LoaderManager in android studio. I am trying to restart the loader whenever search button is press by user.

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Book>>
 mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Check connection status
                checkConnection(cm);
                if (isConnected) {
                    updateQueryUrl(mSearchViewField.getQuery().toString());
                    restartLoader();
                    Log.i(LOG_TAG, "Search value: " + mSearchViewField.getQuery().toString());
                }else{
                    // Clear the adapter of previous book data
                    mAdapter.clear();
                    // Set mEmptyStateTextView visible
                    mEmptyStateTextView.setVisibility(View.VISIBLE);
                    // ...and display message: "No internet connection."
                    mEmptyStateTextView.setText("No Internet Connection");
                }
            }
        });

But, under restartLoader(), when I try to call getLoaderManager() to restart the loader, it's saying that callback argument is wrong, 3rd argument type. I am not sure what should i use for the callback.


public void restartLoader() {
        mEmptyStateTextView.setVisibility(GONE);
        progressBar.setVisibility(View.VISIBLE);
        getLoaderManager().restartLoader(BOOK_LOADER_ID,null, MainActivity.this);
    }

Solution

  • getLoaderManager is deprecated and uses the deprecated framework Loaders.

    You should use LoaderManager.getInstance(MainActivity.this) instead, which uses the correct Support Library/AndroidX Loaders, which is probably the LoaderManager.LoaderCallbacks you've imported.

    LoaderManager.getInstance(MainActivity.this)
        .restartLoader(BOOK_LOADER_ID, null, MainActivity.this);