【发布时间】:2013-04-09 08:56:34
【问题描述】:
根据http://developer.android.com/guide/components/loaders.html 的说法,loader 的优点之一是,它能够在配置更改期间保留其数据。
当它们被加载时,它们会自动重新连接到最后一个加载器的光标 配置更改后重新创建。因此,他们不需要 重新查询他们的数据。
但是,它并不适用于所有场景。
我举一个简单的例子。它是一个FragmentActivity,它托管着一个Fragment。 Fragment 本身拥有 AsyncTaskLoader。
以下 3 个场景运行良好。
首次启动期间(正常)
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
我想知道,这个错误有什么好的解决方法吗?
【问题讨论】:
-
旧的加载器将被销毁 - 这是个问题吗(新的
Loader仍然可以使用相同的数据,对吧)? -
相同数据不再可用。与旧加载器销毁后一样,它之前从 loadInBackground 加载的数据也被销毁。因此,新的 loader 需要再次运行耗时的 loadInBackground。
标签: android