ManagedQuery () vs context.getContentResolver.query () vs android.provider.something.query ()

Pretty simple. What is the difference between the three?

I want to list each image on the device. Should I use managedQuery() , android.provider.MediaStore.Images.Media.query() or context.getContentResolver.query()

+45
android
Apr 7 '10 at 19:15
source share
2 answers

managedQuery() will use the ContentResolver () request. The difference is that with managedQuery() activity will contain a link to your Cursor and close it if necessary (for example, in onDestroy() ). If you do query() yourself, you will be controlled by the cursor as a sensitive resource. If you forget, for example, close() in onDestroy() , you will leak main resources (logcat will warn you about this.)

To request a content provider, you can use either the ContentResolver.query() method or the Activity.managedQuery() method. Both methods accept the same set of arguments, and both return a Cursor object. However, managedQuery() forces the activity to control the Cursor's life cycle. The controlled cursor handles all the subtleties, such as unloading, when activity is paused, and asks for itself when the activity is restarted. You can ask Activity to start managing an unmanaged Cursor object by calling Activity.startManagingCursor() .

Update:

managedQuery now deprecated (since Android 3.0).

+72
Apr 7 '10 at 19:32
source share
โ€” -

managedQuery (..) is now deprecated (since Android 3.0). Beware ..

Android error: java.lang.IllegalStateException: attempt to request an already closed cursor

+23
04 Oct '11 at 20:27
source share



All Articles