Android AsyncTaskLoader doesn't start loadInBackground?

后端 未结 6 1468
有刺的猬
有刺的猬 2021-01-30 10:23

I am trying to implement a loader example on Android but can\'t get it to start the loader. I am using the following code. It will hit the \"Create Loader\" but it will never re

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 10:38

    If you are using a custom loader, you can save the last data reference, and have it available via a getter. when the user rotates his screen, you can get the loader back from getLoaderManager().getLoader method, and then return back the reference. For my testing I noticed that startLoadering goes all the way to CustomLoader.onLoadFinished but the result is never deliver to activity.onLoadFinished.I suspect the activity reference gets lost upon rotation. By the way the great thing about creating loaders is they are persistent through LoaderManager. Think of it as another flavor of headless fragments.. lol.

    Loader loader  =  getLoaderManager().getLoader(LOADER_ID);
    
    if( loader == null )
    {
        getLoaderManager().initLoader(LOADER_ID, null, MyActivity.this );
    }
    else
    {
        listAdapter.addAll(((CustomLoader) loader).getLastData());
    }
    

提交回复
热议问题