Loader mListener (LoaderInfo) is null when device rotation

351 Views Asked by At

My application use an AsyncTaskLoader to query and load from database. User can filter data by categories. It works fine when no device rotation happened, but the Loader does not load any data that filter by category after device orientation changed.

My code is:

//In activity oncreate
getSupportLoaderManager().initLoader (0, null, controller);

on user filter action, I call:

loader.onCententChanged();

I put a break point at above line and point out that in the loader instance, its mListener is NULL so the call onCententChanged() won't deliver result. The mListener is not NULL if we don't rotate the device.

Anyone has this issue? Please help!

EDIT

Sorry for late reply, I don't see any email from Stackoverflow push, I will check my settings.

By the way, my application use MVC pattern, the controller is for handling data load, event listener, manipulation. In this case, the controller implements LoaderManager.LoaderCallbacks. As I debugged it, set breakpoint and see what happening, I see that the mListener member of Loader class is not null when first load in portrait mode but when I turn the device to landscape mode the mListener is NULL so the callback is not call. See the attach image.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_latest_topics_layout);

    // Omitted code

    getSupportLoaderManager().initLoader (0, null, controller);
}

When user filter choose a data filter:

new AlertDialog.Builder(mContext)
            .setTitle("Choose a filter")
            .setSingleChoiceItems(R.array.latest_episodes_filter_titles, mFilter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ListView listView = ((AlertDialog)dialog).getListView();
                    mFilter = listView.getCheckedItemPosition();

                    mListHandler.getLoader().onContentChanged();

                    dialog.dismiss();
                }
            })
            .setNegativeButton("Cancel", null).show();

Debug: When first time load in portrait/landscape

first time load

Debug: when rotate device

then rotate device

1

There are 1 best solutions below

3
On BEST ANSWER

Please try to initialize the loader in the following way. Probably it may help you.

//In activity oncreate
if (getSupportLoaderManager().getLoader(0) == null) {
    getSupportLoaderManager().initLoader(0, new Bundle(), this);
} else {
    getSupportLoaderManager().restartLoader(0, new Bundle(), this);
}