Another option if you still want to use Activity.managedQuery() is to
get your information and then:
stopManaging(cursor);
cursor.close();
Cheers,
Justin
Android Team @ Google
On Aug 21, 1:23 am, "Jeff Hamilton" <[EMAIL PROTECTED]> wrote:
> > So what is the best way to call a Cursor? since i got a meta-database
> > i have quite a few queries where i just get myself a small piece of
> > information and then i don't need the cursor anymore, so i call
> > close() directly after extracting information from it. within an
> > activity, i think the most comfortable way is to call managedQuery()
> > because you don't have to care about exceptions and stuff. but how
> > long will the system try to kkep my cursor available although i don't
> > need it anymore? is the only efficient way to call, get data from and
> > close a cursor through a call to
>
> If you know you only need the info for a short period of time you can
> just call getContentResolver().query(...) to get your cursor and then
> call close() on it as soon as you're done reading the data you need.
> If you're doing something like creating a list of items from a
> database using CursorAdapter it's cleaner to go the managedQuery()
> route since you want the life cycle of the cursor to match the life
> cycle of the activity that's displaying its data.
>
> > try{
> > Cursor c = getContentResolver().acquireProvider(uri).query(...);
> > }catch(RemoteException e){...}
>
> > or are there other ways how i can get a cursor that i can manage
> > myself?
>
> If you're managing the cursor yourself the cleanest way to go is probably
> this:
>
> Cursor cursor = getContentResolver().query(...);
> try {
> // read from the cursor and do stuff} finally {
>
> if (cursor != null) cursor.close();
>
> }
>
> If you're worried about the time the query will take you can also have
> a look
> athttp://code.google.com/android/reference/android/content/AsyncQueryHa....
> That will run the query on a worker thread and call back onto the UI
> thread when the work is done so you can update your UI state safely
> without having to worry about locking.
>
> -Jeff
>
>
>
> > Again, thanks a lot for your useful tips. you saved my ass!
>
> > On Aug 21, 1:58 am, "Jeff Hamilton" <[EMAIL PROTECTED]> wrote:
> >> If you're using a managed cursor you shouldn't be calling close() or
> >> deactivate() on the cursor yourself, since the system will do that for
> >> you. If you're not using a managed cursor and you call close() on it
> >> you cannot use that cursor again and should make sure that you don't
> >> have any observers registered before calling close(), and then
> >> explicitly set all references you hold to the cursor to null just to
> >> make sure you won't use it again. If you plan on reusing the cursor
> >> again, you should call deactivate(), which releases a bunch of
> >> resources, but not all of them. A deactivated cursor can't be used
> >> again until you call requery() on it.
>
> >> -Jeff
>
> >> On Wed, Aug 20, 2008 at 1:36 AM, simon <[EMAIL PROTECTED]> wrote:
>
> >> > Thanks for your help. But i still don't understand exactly what i'm
> >> > doing wrong.
>
> >> > I'm navigating from Activity1 to Activity2. Then i navigate back to
> >> > Activity1 by pushing the Back-Button. You say i should overwrite
> >> > onPause() on Activity2 to release its resources. So the error has
> >> > nothing to do with Activity1? But i'm already using a managed Cursor
> >> > plus i'm calling Cursor.close() in onPause() (just to be sure) in
> >> > Activity2. Other Cursors i am using in other classes also get released
> >> > (by calling close()) immediately after gathering information from
> >> > them. so i don't know what i am doing wrong...is close() the wrong
> >> > function to call?
>
> >> > Do i have to release other resources as well? such as
> >> > ContentProviders? i found out that ContentResolver got a new method
> >> > called releaseProvider(IContentProvider icp). But it's not documented
> >> > yet. should it be used to release the ContentProviders?
>
> >> > just because i'm interested:
> >> > why does the system try to acquire a reference on a Cursor if an
> >> > activity gets closed anyway? i'm getting the following
> >> > RuntimeException (which gets caused by the Exception i posted
> >> > earlier):
>
> >> > java.lang.RuntimeException: Unable to resume activity {my.package/
> >> > my.package.Activity2}: java.lang.IllegalStateException: attempt to
> >> > acquire a reference on a close SQLiteClosable
>
> >> > Why does the system tell me that it's unable to resume an Activity
> >> > which i just wanted to close by pushing the back button? and what is a
> >> > "close SQLiteClosable"? shouldn't it read "closeD SQLiteClosable?" And
> >> > if i release Cursors by calling close(), aren't these also "close
> >> > SQLiteClosables" which can't be acquired? i just dont get it...
>
> >> > I would be happy to get more information about what the system is
> >> > doing between the Activities and why those strange error messages
> >> > occur
>
> >> > Thanks a lot
> >> > Simon
>
> >> > On Aug 19, 6:05 pm, "Justin (Google Employee)" <[EMAIL PROTECTED]>
> >> > wrote:
> >> >> My guess would be that you have an SQLite cursor that you're not
> >> >> releasing when your Activity loses focus. I believe you have two
> >> >> options.
>
> >> >> You say that you've tried to handle this in callback functions
> >> >> onRestart, onResume, etc. The problem is you're looking at the wrong
> >> >> end of the lifecycle. When the user hits the 'back' key, onPause is
> >> >> called. In onPause you should handling releasing resources and any
> >> >> other tear-down you need to do. I'd suggest reviewing the Activity
> >> >> lifecycle information
> >> >> (http://code.google.com/android/reference/android/app/Activity.html
> >> >> ) .
>
> >> >> The other option is to use a managed cursor. To do this call
> >> >> Activity.startManagingCursor(yourCursor).
>
> >> >> Cheers,
> >> >> Justin
> >> >> Android Team @ Google
>
> >> >> On Aug 19, 7:16 am, simon <[EMAIL PROTECTED]> wrote:
>
> >> >> > Hi all!
> >> >> > First of all i wanted to say thank you to Google for the new SDK!
> >> >> > Lookin forward to the phones!
>
> >> >> > now to my problem:
> >> >> > as soon as i push the back button in some of my application's
> >> >> > activities, i get the following error:
>
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): Caused by:
> >> >> > java.lang.IllegalStateException: attempt to acquire a reference on a
> >> >> > close SQLiteClosable
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:
> >> >> > 31)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:109)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:129)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:389)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.database.CursorWrapper.requery(CursorWrapper.java:214)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.app.Activity.performRestart(Activity.java:3330)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.app.Activity.performResume(Activity.java:3350)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): at
> >> >> > android.app.ActivityThread.performResumeActivity(ActivityThread.java:
> >> >> > 2482)
> >> >> > 08-19 14:10:13.050: ERROR/AndroidRuntime(1183): ... 10 more
>
> >> >> > What does that mean? and is there a workaround? i already tried
> >> >> > different callback functions (onRestart(), onResume(),
> >> >> > onRestoreInstanceState()) but none gets called before the error
> >> >> > occurs. how can i do something before that error occurs? and what
> >> >> > should i do against it once i get a callback? i don't understand the
> >> >> > error message. it worked on m5, but 0.9 just won't do it.
>
> >> >> > please help me. Thanx a lot!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---