The reason for this slowdown is that you are most likely requesting from the contact provider on the phone, extracting some data from these requests, placing them in mPeopleList , and then installing it on your SimpleAdapter . So your onCreate activity onCreate waits until PopulatePeopleList() finishes its work. I donβt know how you request this contact provider, but see if you can adapt your code to use CursorLoader (an older version of Android is available through the compatibility package). This will mean that you have to switch to a Cursor based adapter, other changes are possible depending on your code.
If you still want to use a non-based SimpleAdapter , you need to override it to implement your own AsyncTaskLoader (again available in the old version of Android via the compatibility package):
public class ContactsDataLoader extends AsyncTaskLoader<ArrayList<Map<String, String>>> { public ContactsDataLoader(Context context) { super(context); } @Override public ArrayList<Map<String, String>> loadInBackground() {
Then you will have activity where you need this data, implement LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>> :
public class MainActivity implements LoaderManager.LoaderCallbacks<ArrayList<Map<String, String>>>
who needs these methods:
@Override public Loader<ArrayList<Map<String, String>>> onCreateLoader(int id, Bundle args) { return new ContactsDataLoader(context); } @Override public void onLoadFinished(Loader<ArrayList<Map<String, String>>> loader, ArrayList<Map<String, String>> data) {
Then you only need to call:
in your onCreate method. The application starts up pretty quickly, but at first it will be empty until the bootloader deals with it and receives data from the contacts and installs it on your adapter.
source share