Is it OK to have one instance of SQLiteOpenHelper shared by all Activities in an Android application?

Click here to see my blog post on this subject. CommonsWare is right on (as usual). Expanding on his post, here is some sample code that illustrates three possible approaches. These will allow access to the database throughout the application. Approach #1: subclassing `Application` If you know your application won’t be very complicated (i.e. if … Read more

How do CursorLoader automatically updates the view even if the app is inactive?

I found the answer for my question. In general, CursorLoader doesn’t automatically detect data changes and load them to view. We need to track URI for changes. This can be done by following steps: Registering an Observer in content resolver through cursor using: (Done in the query method of ContentProvider) cursor.setNotificationUri(getContext().getContentResolver(), uri); Now when there … Read more

CursorLoader not updating after data change

Did you call setNotificationUri(ContentResolver cr, Uri uri) on the Cursor before returning it in ContentProvider.query()? And did you call getContext().getContentResolver().notifyChange(uri, null) in the ‘insert’ method of your ContentProvider? EDIT: To get a ContentResolver call getContext().getContentResolver() in your ContentProvider.

AlphabetIndexer with Custom Adapter managed by LoaderManager

So I finally got this to work. Here’s how i did it: I added: ListView lv = getListView(); lv.setFastScrollEnabled(true); lv.setScrollingCacheEnabled(true); to the onLoadFinished() method after the new cursor was swapped in like so public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Swap the new cursor in. (The framework will take care of closing the // … Read more

CursorLoader usage without ContentProvider

I wrote a simple CursorLoader that does not need a content provider: import android.content.Context; import android.database.Cursor; import android.support.v4.content.AsyncTaskLoader; /** * Used to write apps that run on platforms prior to Android 3.0. When running * on Android 3.0 or above, this implementation is still used; it does not try * to switch to the framework’s … Read more