Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loader restarts on orientation change

Tags:

android

In the Android documentation for Loaders found at http://developer.android.com/guide/components/loaders.html it says one of the properties of loaders is that:

They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.

The following code does not seem to mirror that behaviour, a new Loader is created an finishes querying the ContentResolver, then I rotate the screen and the Loader is re-created!

public class ReportFragment extends Fragment implements LoaderCallbacks<Cursor> {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLoaderManager().initLoader(1, null, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_report, container, false);
        return v;
    }

    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        Log.d("TEST", "Creating loader");
        return new CursorLoader(getActivity(), ResourcesContract.Reports.CONTENT_URI, null, null, null, null);
    }

    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        Log.d("TEST", "Load finished");
    }

    public void onLoaderReset(Loader<Cursor> arg0) {

    }

}

Here is the output from my logcat:

08-17 16:49:54.474: D/TEST(1833): Creating loader
08-17 16:49:55.074: D/TEST(1833): Load finished
*Here I rotate the screen*
08-17 16:50:38.115: D/TEST(1833): Creating loader
08-17 16:50:38.353: D/TEST(1833): Load finished

Any idea what I'm doing wrong here?

EDIT:

I should note that I'm building to Android Google API's version 8, and using the v4 support library.

2nd EDIT:

This is most likely due to a bug in the support library, take a look at this bug submission if you want further information:

http://code.google.com/p/android/issues/detail?id=20791&can=5&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

like image 802
soren.qvist Avatar asked Aug 17 '12 16:08

soren.qvist


2 Answers

Though this is an old question, I've been experiencing the same issue as the OP. Using a loader, I need to have it restarting when navigating to a new Activity, and then back. But at the same time, I don't want the loader to restart when I rotate the phone's screen.

What I found is that it is possible to achieve this in onRestart(), if you restart the loader BEFORE calling its super.

public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        //Initialize the loader.
        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    protected void onRestart() {
        //Restart the loader before calling the super.
        getSupportLoaderManager().restartLoader(LOADER_ID, null, this);

        super.onRestart();
    }

    ...

}
like image 75
LifeOfAStudent Avatar answered Nov 03 '22 00:11

LifeOfAStudent


In my opinion you misunderstood what the documentation says. The documentations says, that they don't need to re-query their data, and it is not doing so.

Try to log/insert a breakpoint in your ContentProvider#query() method! The query will be called only on Activity startup, and not after orientation change.

But this is not true for the LoaderCallbacks#onCreateLoader() method. It will be called after every orientation change, but this not means re-querying, it just calls the method so you can change the CursorLoader if you want.

like image 34
bendaf Avatar answered Nov 03 '22 00:11

bendaf