Search code examples
androidfragmentandroid-loadermanager

LoaderManager throwing incompatible types error


I am calling AsyncTaskLoader in one of the fragments I created. I have imported import android.app.LoaderManager; and I am getting error while calling this method LoaderManager manager = getLoaderManager();. I have the same thing going in my main activity and there it doesn't throw any error.

It also ask me to import this android.support.v4.app.LoaderManager instead of android.support.v4.app.LoaderManager. When I did, it showing the getLoaderManager() is deprecated.

My FragmentExchangeList.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class FragmentExchangeList extends Fragment implements   
LoaderManager.LoaderCallbacks<ArrayList<ExchangeListData>> {              
    private List<ExchangeListData> mExchangeListData;
    private ListView mExchangeListView;
    private AutoCompleteTextView mSetbaseCurrency;
    private String linkForCurrencyList;
    private static final int CURRENCY_List_LOADER_ID = 2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_exchange_list, container, false);

        mExchangeListData = new ArrayList<ExchangeListData>();
        mExchangeListView = rootView.findViewById(R.id.exchange_list_view);
        mSetbaseCurrency = rootView.findViewById(R.id.set_base_currency);

        linkForCurrencyList = "https://ratesapi.io/api/latest?base="+mSetbaseCurrency.getText().toString();
        LoaderManager manager = getLoaderManager();
        manager.initLoader(CURRENCY_List_LOADER_ID, null, this);

        return rootView;
    }


    @Override
    public Loader<ArrayList<ExchangeListData>> onCreateLoader(int i, Bundle bundle) {
        return new ExchangeListBackground(getContext(), linkForCurrencyList);
    }

    @Override
    public void onLoadFinished(Loader<ArrayList<ExchangeListData>> loader, ArrayList<ExchangeListData> data) {
        mExchangeListData.clear();
        if(data != null && !data.isEmpty()){
            mExchangeListData.addAll(data);
        }
    }

    @Override
    public void onLoaderReset(Loader<ArrayList<ExchangeListData>> loader) {
        mExchangeListData.clear();
    }
}

The error is error: incompatible types: android.support.v4.app.LoaderManager cannot be converted to android.app.LoaderManager


Solution

  • I had the same problem in the past. The fragment that you are using is from support library, so you need to use the android.support.v4.app.LoaderManager package to the LoadManager too.

    But searching about the deprecation, is because google is deprecating all the stack of loaders, They are recommending to use Jobs. So, to solve your problem use the deprecated package and put a suppress deprecated comment to Android Studio not be annoying.