I can not figure out how to organize the save when you rotate the screen. I get information 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(); } } 
  • You did not describe exactly what your problem is. Loader should not lose data when you rotate the screen. - pavlofff
  • each time the screen is rotated, the data is loaded, so I will not figure out how to avoid it and load the data already received before - UjinUkr

1 answer 1

forceLoad () - “forced” loading of new data, and you call this method in onCreate, which means that with each turn of the screen. Call forceLoad in the overridden "onStartLoading" method

  private static class FetchWeatherLoader extends AsyncTaskLoader<String> { public FetchWeatherLoader(Context context) { super(context); } @Override protected void onStartLoading() { forceLoad(); } @Override public List<WeatherItem> loadInBackground() { return new OpenWeatherFetch().fetchItems(); } } 
  • without forseLoad does not load at all and does not cause onLoadFinished, only onCreateLoader works - UjinUkr
  • Corrected, checked, works. - Nikolai Konorev