根据http://developer.android.com/guide/components/loaders.html,loader 的好处之一是,它能够在配置更改期间保留其数据。
在配置更改后重新创建时,它们会自动重新连接到最后一个加载器的光标。因此,他们不需要重新查询他们的数据。
但是,它并不适用于所有场景。
我举一个简单的例子。它是一个FragmentActivity
,它正在托管一个Fragment
. 本身Fragment
拥有.AsyncTaskLoader
以下 3 个场景运行良好。
首次启动期间 (OK)
创建了 1 个加载器,并loadInBackground
执行一次。
简单旋转时(OK)
没有创建新的加载程序,loadInBackground
也没有被触发。
启动子活动,并按下后退按钮(确定)
没有创建新的加载程序,loadInBackground
也没有被触发。
但是,在以下场景中。
启动子活动 -> 旋转 -> 按下后退按钮(错误)
那时,旧装载机onReset
被称为。旧的装载机将被销毁。将创建新的加载器,并 loadInBackground
再次触发新的加载器。
我期望的正确行为是,不会创建新的加载器。
loader相关代码如下。我在 Android 4.1 模拟器下运行代码。
package com.example.bug;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment implements LoaderManager.LoaderCallbacks<Integer> {
private static class IntegerArrayLoader extends AsyncTaskLoader<Integer> {
private Integer result = null;
public IntegerArrayLoader(Context context) {
super(context);
Log.i("CHEOK", "IntegerArrayLoader created!");
}
@Override
public Integer loadInBackground() {
Log.i("CHEOK", "Time consuming loadInBackground!");
this.result = 123456;
return result;
}
/**
* Handles a request to cancel a load.
*/
@Override
public void onCanceled(Integer integer) {
super.onCanceled(integer);
}
/**
* Handles a request to stop the Loader.
* Automatically called by LoaderManager via stopLoading.
*/
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
/**
* Handles a request to start the Loader.
* Automatically called by LoaderManager via startLoading.
*/
@Override
protected void onStartLoading() {
if (this.result != null) {
deliverResult(this.result);
}
if (takeContentChanged() || this.result == null) {
forceLoad();
}
}
/**
* Handles a request to completely reset the Loader.
* Automatically called by LoaderManager via reset.
*/
@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.
this.result = null;
}
}
@Override
public Loader<Integer> onCreateLoader(int arg0, Bundle arg1) {
Log.i("CHEOK", "onCreateLoader being called");
return new IntegerArrayLoader(this.getActivity());
}
@Override
public void onLoadFinished(Loader<Integer> arg0, Integer arg1) {
result = arg1;
}
@Override
public void onLoaderReset(Loader<Integer> arg0) {
// TODO Auto-generated method stub
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
// http://stackoverflow.com/questions/11293441/android-loadercallbacks-onloadfinished-called-twice
@Override
public void onResume()
{
super.onResume();
if (result == null) {
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
} else {
// Restore from previous state. Perhaps through long pressed home
// button.
}
}
private Integer result;
}
完整的源代码可以从https://www.dropbox.com/s/n2jee3v7cpwvedv/loader_bug.zip下载
这可能与 1 个未解决的 Android 错误有关:https ://code.google.com/p/android/issues/detail?id=20791&can=5&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/DbKL6PVyhLI
我想知道,这个错误有什么好的解决方法吗?