getLoaderManager()initLoader();使用support.v4.fragment时总是返回一个新的加载器

时间:2015-09-30 23:43:38

标签: android android-fragments asynctaskloader

我使用android.support.v4.content.AsyncTaskLoader将数据加载到support.v4.fragment但是当配置更改时,即:旋转屏幕getLoaderManager().initLoader();始终返回一个新的加载器,因此调用loadInBackground()再次。当我尝试使用普通fragment而不是support.v4版本并更改为正常AsyncTaskLoader时,每件事情都按预期工作,所以我不确定这是否是一个错误支持库还是什么?

TestAsync.class:

import android.support.v4.content.AsyncTaskLoader;
public class TestAsync extends AsyncTaskLoader<List<Movie>> {
    public TestAsync(Context context) {
        super(context);
    }
    @Override
    public void deliverResult(List<Movie> data) {
        if (isReset()) {
            // An async query came in while the loader is stopped.  We
            // don't need the result.
            if (data != null) {
                onReleaseResources(data);
            }
        }
        List<Movie> oldData = mData;
        mData = data;

        if (isStarted()) {
            // If the Loader is currently started, we can immediately
            // deliver its results.
            super.deliverResult(data);
        }
        // At this point we can release the resources associated with
        // 'oldApps' if needed; now that the new result is delivered      we
        // know that it is no longer in use.
        if (oldData != null) {
            onReleaseResources(oldData);
        }
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();

        if (mData != null || oneShot){

            deliverResult(mData);
        }else {
            forceLoad();
        }

    } 
    @Override
    protected void onStopLoading() {
        cancelLoad();
    }


    @Override public void onCanceled(List<Movie> data) {
        super.onCanceled(data);

        // At this point we can release the resources associated with 'apps'
        // if needed.
        onReleaseResources(data);
    }

    /**
     * Handles a request to completely reset the Loader.
    */
    @Override protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        // At this point we can release the resources associated with 'apps'
        // if needed.
        if (mData != null) {
            onReleaseResources(mData);
            mData = null;
        }
    }
    protected void onReleaseResources(List<Movie> apps) {
        // For a simple List<> there is nothing to do.  For something
        // like a Cursor, we would close it here.
    }
}

BrowseMoviesActivityFragment:

public class BrowseMoviesActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Movie>> {


   @Override
    public Loader<List<Movie>> onCreateLoader(int id, Bundle args) {

        return new TestAsync(mContext);
    }

    @Override
    public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> data) {

        if (data != null) {
            if (adapter.isEmpty()){
                adapter.add(data);
                gridView.setAdapter(adapter);
            }else {
                adapter.add(data);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<List<Movie>> loader) {
        adapter = new BrowseMoviesAdapter(getActivity(),new ArrayList<Movie>());
    }
    @Override
     public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
     }

      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      Bundle args = new Bundle();
      getLoaderManager().initLoader(id, args, this);
      }
}

BrowseMoviesActivity:

import android.support.v7.app.AppCompatActivity;
public class BrowseMoviesActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browse);
            android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
            BrowseMoviesActivityFragment browseFragment = (BrowseMoviesActivityFragment) fm.findFragmentByTag(TAG_BROWSE_FRAGMENT);
            if (savedInstanceState == null){
                if (browseFragment==null){
                    browseFragment = new BrowseMoviesActivityFragment();
                    fm.beginTransaction().replace(R.id.browse_container,   browseFragment).commit();
            }
        }

0 个答案:

没有答案