Question

I have ListFragment (from support package) and I am trying use adapter with Sprinkles library (CursorList). Everything works fine till I change screen orientations. Then I get exception:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: select * from MyTable

I was inspired from sample app on github (https://github.com/emilsjolander/sprinkles/tree/master/sample).

At sample query is registered in onCreate function in Activity and I register it in onAttach function in my ListFragment.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    ...
    ...

    adapter = new MyAdapter(getActivity());
    Query.many(MyClass.class, "select * from myTable")
         .getAsync(getLoaderManager(), onMyClassLoaded, MyClass.class);
}

My callback is the same as sample one.

private ManyQuery.ResultHandler<MyClass> onMyClassLoaded =
        new ManyQuery.ResultHandler<MyClass>() {

            @Override
            public boolean handleResult(CursorList<MyClass> result) {
                adapter.swapCursorList(result);
                return true;
            }
        };

I debug what's going on after orientation change and callback onMyClassLoaded is called twice (sometimes even more times), but first creating of adapter call onMyClassLoaded just one times. The problem is why callback function is called more times after orientation change.

Any help clarifying this issue will be very appreciated!

Was it helpful?

Solution

Finally I found the problem myself so I give it as answer. Maybe someone will need it, because I found nothing about this matter on Sprinkle's GitHub or Google.

If you want use Sprinkles inside Fragment so you have to change a little the query.

Instead of

Query.many(MyClass.class, "select * from myTable")
     .getAsync(getLoaderManager(), onMyClassLoaded, MyClass.class);

use LoaderManager from your activity.

in my case:

Query.many(MyClass.class, "select * from myTable")
     .getAsync(getActivity().getSupportLoaderManager(), onMyClassLoaded, MyClass.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top