Android Loadermanager onloadfinished method was not called on Orientation Change when it is declared outside on create method

StackOverflow https://stackoverflow.com/questions/21404835

Question

i am initializing the loader in action bar navigation list item callback method. By default first item will be selected in action bar navigation list. based on navitem selection i am initializing the loader. at the launch of application the loader call back methods are calling fine. but when i change the orientation the loader callback methods are not getting called. but if i initialize the loader in oncreate method the loader callback methods are getting called after orientation change also.

My Code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_base);
 getActionBar().setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {

                    @Override
                    public boolean onNavigationItemSelected(int itemPosition, long itemId) {



                         if(itemPosition == 0 || itemPosition ==1)
                        {
                            Bundle b = new Bundle();
                            b.putString(Constatnts.Url, serviceurl +"/getgroups/"+shared.getString("StationID", "null")+"/0");
                            b.putInt(Constatnts.selection, itemPosition);
                            b.putString(Constatnts.xmlroles, session.getUserDetails().get(SessionManager.KEY_NAME).get(1));
                            getLoaderManager().initLoader(0, b, MainActivity.this);
                        }

                        else if(itemPosition == 2)
                        {

                            Bundle b = new Bundle();
                            b.putString(Constatnts.Url, serviceurl +"/getchanneldetails/"+shared.getString("StationID", "null")+"/1/0");
                            b.putInt(Constatnts.selection, itemPosition);
                            b.putString(Constatnts.xmlroles, session.getUserDetails().get(SessionManager.KEY_NAME).get(1));
                            getLoaderManager().initLoader(itemPosition, b, MainActivity.this);
                        }
                        else
                        {

                            Bundle b = new Bundle();
                            b.putString(Constatnts.Url, serviceurl+"/Getcategories/"+shared.getString("StationID", "null")+"");
                            b.putInt(Constatnts.selection, itemPosition);
                            b.putString(Constatnts.xmlroles, session.getUserDetails().get(SessionManager.KEY_NAME).get(1));
                            getLoaderManager().initLoader(itemPosition, b, MainActivity.this);

                        }

                        return true;
                    }
                });
}

The loader callback methods are not getting called after orientation change with the above method.

*but if i place the below code outside the navigationlistcallbacks and inside oncreate method the loader callback methods are getting called *

Bundle b = new Bundle();
                                b.putString(Constatnts.Url, serviceurl +"/getgroups/"+shared.getString("StationID", "null")+"/0");
                                b.putInt(Constatnts.selection, itemPosition);
                                b.putString(Constatnts.xmlroles, session.getUserDetails().get(SessionManager.KEY_NAME).get(1));
                                getLoaderManager().initLoader(0, b, MainActivity.this);

how can i reload the data after orientation change if initloader is in setListNavigationCallbacks methods.

Was it helpful?

Solution

The callbacks are only invoked after the loader is initiated (initLoader)or restarted (restartLoader). With initLoader the cached result can be used after an orientation change but it has to be called explicitly to deliver the data in onLoadFinished. Hence, you have to call it in your onCreate method with the itemPosition as the loader id, but only if an itemPosition is already set.

I've stripped your example code to illustrate what I mean:

// static so that it survives orientation change.  
private static int mSelectedItemPosition = -1; // -1 = Not selected

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mSelectedItemPosition != -1) {
        getLoaderManager().initLoader(mSelectedItemPosition, null, MainActivity.this);
    }

    getActionBar().setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener()       {

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    if(itemPosition == 0 || itemPosition ==1) {
        mSelectedItemPosition = 0;
        Bundle b = ...
        getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
    }
    else if(itemPosition == 2) {
        mSelectedItemPosition = itemPosition;
        Bundle b = ...
        getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
    }
    else {
        mSelectedItemPosition = itemPosition;
        Bundle b = ...
        getLoaderManager().initLoader(mSelectedItemPosition, b, MainActivity.this);
    }
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top