Kay Gladen
Kay Gladen

Reputation: 1730

How to close database in Cursorloader

I'm currently working on ListViews in Fragments. The Listviews are loaded by Cursorloader, but without ContentManager. So the code looks like this and it works:

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

    Log.d("SoapERP", "onCreateLoader");
    CursorLoader loader = new CursorLoader(getActivity()) {
    final DBHelper dbhelper1= new DBHelper(getActivity());       
        @Override
        public Cursor loadInBackground() {
            Cursor c = null;
            dbhelper1.open();
            c = dbhelper1.fetchAllMatnameswithID();
//              dbhelper1.close();
            return c;

        }

     };
    return loader;

My problem is that I get an LogCat-Error-Message that the database wasn't closed. But if I use dbhelper.close(); I get the Error "Database is already closed" wich is also understandable because it is just before the return statement. After the return statement code is not reachable and if I declare DBHelper dbhelper1 final the program crashes without any information in logcat. So what is my fail???

Upvotes: 0

Views: 875

Answers (1)

Kay Gladen
Kay Gladen

Reputation: 1730

Finally I found here the right statement as of Dianne Hackborn from android framework development: "A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database -- it will get closed as part of the kernel cleaning up the process's resources when the process is killed. Dianne Hackborn Android framework engineer [email protected] " - so let's use Content Provider.

Upvotes: 1

Related Questions