Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
75 views11 pages

Loaders - Android Developers

The document discusses Android loaders which allow loading data from a content provider or other source asynchronously in the background. Loaders can be restarted if the configuration changes and callbacks allow updating UI when loading is complete. CursorLoader is a type of loader that queries and loads cursor data from a content provider.

Uploaded by

Moshe Rosten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views11 pages

Loaders - Android Developers

The document discusses Android loaders which allow loading data from a content provider or other source asynchronously in the background. Loaders can be restarted if the configuration changes and callbacks allow updating UI when loading is complete. CursorLoader is a type of loader that queries and loads cursor data from a content provider.

Uploaded by

Moshe Rosten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Loaders | Android Developers

1 of 11

Developers
Training

https://developer.android.com/guide/components/loaders.html

Design

API Guides Reference

Develop

Tools

Console
Distribute

Google Services

Samples

Introduction

App Components

Intents and Intent Filters

Loader API Summary


Activities

Using Loaders in an
Application
Starting a Loader

Fragments

Restarting a Loader

Loaders

Activity
Tasks and Back Stack

Fragment

Using the LoaderManager


Callbacks
Example

Overview Screen

More Examples
Services

Content Providers

LoaderManager
Loader

App Widgets

Processes and Threads

LoaderCursor
LoaderThrottle

Class/Interface

Description

LoaderManager
Activity

Fragment

12/5/2015 11:05 AM

Loaders | Android Developers

2 of 11

https://developer.android.com/guide/components/loaders.html

Loader

Activity
Fragment

CursorLoader

LoaderManager

LoaderManager

LoaderManager.LoaderCallbacks
LoaderManager

onCreateLoader()

Loader

CursorLoader

AsyncTaskLoader
AsyncTask
CursorLoader

AsyncTaskLoader
ContentResolver
Cursor
Loader

AsyncTaskLoader

12/5/2015 11:05 AM

Loaders | Android Developers

3 of 11

https://developer.android.com/guide/components/loaders.html

ContentProvider

LoaderManager
Loader
CursorLoader

Activity

Fragment
LoaderManager

CursorLoader

ContentProvider
Loader

AsyncTaskLoader

LoaderManager.LoaderCallbacks

SimpleCursorAdapter
ContentProvider

LoaderManager
Activity

CursorLoader

Loader

Fragment

Loader

LoaderManager

onCreate()

onActivityCreated()

12/5/2015 11:05 AM

Loaders | Android Developers

4 of 11

https://developer.android.com/guide/components/loaders.html

// Prepare the loader. Either re-connect with an existing one,


// or start a new one.
getLoaderManager().initLoader(0, null, this);

initLoader()

null

LoaderManager.LoaderCallbacks
LoaderManager
LoaderManager.LoaderCallbacks
this
initLoader()

initLoader()
LoaderManager.LoaderCallbacks

onCreateLoader()

onCreateLoader
LoaderManager.LoaderCallbacks

onLoadFinished()

initLoader()

onLoadFinished

initLoader()

Loader
LoaderManager
LoaderManager

LoaderThrottle
LoaderManager.LoaderCallbacks

12/5/2015 11:05 AM

Loaders | Android Developers

5 of 11

https://developer.android.com/guide/components/loaders.html

Using the LoaderManager Callbacks

initLoader()

restartLoader()
SearchView.OnQueryTextListener

public boolean onQueryTextChanged(String newText) {


// Called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query
// with this filter.
mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return true;
}

LoaderManager.LoaderCallbacks
LoaderManager
CursorLoader

onStop()

onStart()

LoaderManager.LoaderCallbacks

LoaderManager.LoaderCallbacks
onCreateLoader()

Loader

onLoadFinished()

onLoaderReset()

12/5/2015 11:05 AM

Loaders | Android Developers

6 of 11

https://developer.android.com/guide/components/loaders.html

initLoader()

LoaderManager.LoaderCallbacks

onCreateLoader()
CursorLoader

Loader
onCreateLoader()
CursorLoader

CursorLoader

ContentProvider

null

null

null

// If non-null, this is the current filter the user has provided.


String mCurFilter;
...
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;

12/5/2015 11:05 AM

Loaders | Android Developers

7 of 11

https://developer.android.com/guide/components/loaders.html

}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

CursorLoader
close()

CursorAdapter
swapCursor()

Cursor

// This is the Adapter being used to display the list's data.


SimpleCursorAdapter mAdapter;
...
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}

swapCursor()

null

// This is the Adapter being used to display the list's data.


SimpleCursorAdapter mAdapter;

12/5/2015 11:05 AM

Loaders | Android Developers

8 of 11

https://developer.android.com/guide/components/loaders.html

...
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}

Fragment
ListView
CursorLoader

READ_CONTACTS

public static class CursorLoaderListFragment extends ListFragment


implements OnQueryTextListener, LoaderManager.LoaderCallbacks
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
// If non-null, this is the current filter the user has provided.
String mCurFilter;
@Override public void onActivityCreated(Bundle savedInstanceState
super.onActivityCreated(savedInstanceState);
// Give some text to display if there is no data.
// application this would come from a resource.
setEmptyText("No phone numbers");

In a real

// We have a menu item to show in action bar.


setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, null,
new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS
new int[] { android.R.id.text1, android.R.id.text2
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}

12/5/2015 11:05 AM

Loaders | Android Developers

9 of 11

https://developer.android.com/guide/components/loaders.html

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater


// Place an action bar item for searching.
MenuItem item = menu.add("Search");
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView sv = new SearchView(getActivity());
sv.setOnQueryTextListener(this);
item.setActionView(sv);
}
public boolean onQueryTextChange(String newText) {
// Called when the action bar search text has changed. Update
// the search filter, and restart the loader to do a new query
// with this filter.
mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return true;
}
@Override public boolean onQueryTextSubmit(String query) {
// Don't care about this.
return true;
}
@Override public void onListItemClick(ListView l, View v, int position
// Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id);
}
// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.CONTACT_STATUS,
Contacts.CONTACT_PRESENCE,
Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY,
};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;
}

12/5/2015 11:05 AM

Loaders | Android Developers

10 of 11

https://developer.android.com/guide/components/loaders.html

// Now create and return a CursorLoader that will take care of


// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
}

LoaderCursor
LoaderThrottle

Getting the
Samples

Get news & tips

Blog Support

Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details

12/5/2015 11:05 AM

Loaders | Android Developers

11 of 11

https://developer.android.com/guide/components/loaders.html

and restrictions, see the Content License.


About Android | Auto | TV | Wear | Legal

English

12/5/2015 11:05 AM

You might also like