我尝试使用Loaders而不是AsyncTask.I无法弄清楚如何在旋转屏幕时正确组织保存状态。我从Json收到信息并尝试创建列表。
public class FragmentWeatherTestApplication extends Fragment
implements LoaderManager.LoaderCallbacks<List<WeatherItem>> {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(0,null,this).forceLoad();
}
...
@Override
public Loader<List<WeatherItem>> onCreateLoader(int id, Bundle args) {
Log.i(TAG, "onCreateLoader");
return new FetchWeatherLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<WeatherItem>> loader, List<WeatherItem> data) {
Log.i(TAG, "onLoadFinished");
mItems = data;
setupAdapter();
}
@Override
public void onLoaderReset(Loader<List<WeatherItem>> loader) {
Log.i(TAG, "onLoaderReset");
}
....
private static class FetchWeatherLoader extends AsyncTaskLoader<List<WeatherItem>>{
public FetchWeatherLoader(Context context) {
super(context);
}
@Override
public List<WeatherItem> loadInBackground() {
return new OpenWeatherFetch().fetchItems();
}
}