Android shuts down the cursor code method in an asynchronous task _android

Source: Internet
Author: User

Querying data can be time-consuming, so we want to put the query data in an asynchronous task, query results get Cursor, and then set adapter in the OnPostExecute (Cursor result) method, We may think of using the managedquery of activity to generate cursor so that cursor will be consistent with Acitivity's life cycle, and how perfect the solution is! However, in fact Managedquery also has a lot of limitations, Managedquery generated cursor must ensure that it will not be replaced, because many programs are actually query conditions are uncertain, so we often use the cursor of the new query to replace the original cursor. Therefore, the scope of this method is also very small.

We can't shut down the cursor directly, but note that CursorAdapter does not automatically shut down the cursor at the end of acivity, so you need to turn it off manually in the OnDestroy function.

Copy Code code as follows:

@Override
protected void OnDestroy () {
Super.ondestroy ();
Mphotoloader.stop ();
if (madapter!= null && madapter.getcursor ()!= null) {
Madapter.getcursor (). Close ();
}
}

If cursor is not used in adapter, you can manually turn off cursor.

Copy Code code as follows:

Cursor Cursor = null;
try{
cursor = Mcontext.getcontentresolver (). query (Uri,null,null,null,null);
if (cursor!= null) {
Cursor.movetofirst ();
Do something
}
}catch (Exception e) {
E.printstatcktrace ();
}finally{
if (cursor!= null) {
Cursor.close ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.