问题描述
在Android的文档在http://developer.android.com/guide/components/loaders.html它说装载机的特性之一是:
In the Android documentation for Loaders found at http://developer.android.com/guide/components/loaders.html it says one of the properties of loaders is that:
他们自动重新连接到最后装载器的游标时配置更改后正在重建。因此,它们并不需要重新查询他们的数据。
下面code似乎并没有反映这种行为,一个新的Loader在创建完成查询ContentResolver的,那么我旋转屏幕,并重新创建的装载机!
The following code does not seem to mirror that behaviour, a new Loader is created an finishes querying the ContentResolver, then I rotate the screen and the Loader is re-created!
public class ReportFragment extends Fragment implements LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(1, null, this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_report, container, false);
return v;
}
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Log.d("TEST", "Creating loader");
return new CursorLoader(getActivity(), ResourcesContract.Reports.CONTENT_URI, null, null, null, null);
}
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
Log.d("TEST", "Load finished");
}
public void onLoaderReset(Loader<Cursor> arg0) {
}
}
下面是从我的logcat的输出:
Here is the output from my logcat:
08-17 16:49:54.474: D/TEST(1833): Creating loader
08-17 16:49:55.074: D/TEST(1833): Load finished
*Here I rotate the screen*
08-17 16:50:38.115: D/TEST(1833): Creating loader
08-17 16:50:38.353: D/TEST(1833): Load finished
任何想法,我在做什么错在这里?
Any idea what I'm doing wrong here?
编辑:
我要指出,我要建到Android谷歌API的版本8,并使用V4支持库。
I should note that I'm building to Android Google API's version 8, and using the v4 support library.
2日编辑:
这是最有可能是由于在支持库中的错误,看看这个bug提交,如果您想进一步的信息:
This is most likely due to a bug in the support library, take a look at this bug submission if you want further information:
<一个href="http://$c$c.google.com/p/android/issues/detail?id=20791&can=5&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars">http://$c$c.google.com/p/android/issues/detail?id=20791&can=5&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
推荐答案
的onCreate()在屏幕方向的变化被称为活动以来被破坏并重新创建。
onCreate() gets called during screen orientation change since the activity gets destroyed and recreated.
除非你装载大量的数据,那么它不伤害做的,但你可以试试下面的,如果你想(我没有测试过,但在理论上,我认为这是可行的)。
Unless you're loading a lot of data then it doesn't hurt to do, but you can try the following if you want (I haven't tested it, but in theory I think it would work).
声明静态布尔在全球顶级的类。我的认为的,你还需要一个静态游标引用
Declare a static boolean at the top global of the class. I think you'll also need a static cursor to reference
private static boolean dataDownloaded = false;
private static Cursor oldCursor;
然后在onLoadFinished集dataDownloaded =真
Then on onLoadFinished set dataDownloaded = true
覆盖的onSaveInstanceState保存布尔值
Override onSaveInstanceState to save the boolean value
@Override
protected void onSaveInstanceState(Bundle outSave) {
outSave.putBoolen("datadownloaded", dataDownloaded);
oldCursor = adapter.swapCursor(null);
}
和的onCreate添加以下
and onCreate add the following
if (savedInstanceState != null) {
this.dataDownloaded = savedInstanceState.getBoolean("datadownloaded", false);
}
调整onCreateLoader
adjust your onCreateLoader
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader;
if (dataDownloaded) {
cursorLoader = new CursorLoader(getActivity(),
null, projection, null, null, null);
cursorLoader.deliverResult(oldCursor);
} else {
CursorLoader cursorLoader = new CursorLoader(getActivity(),
URI_PATH, projection, null, null, null);
}
return cursorLoader;
}
这篇关于装载机重新启动的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!