使用LoaderManager.LoaderCallbacks <cursor>实现旋转设备后的CursorIndexOutOfBoundsException

时间:2015-09-24 20:29:20

标签: android android-cursorloader

推出例外:

  • 之后 getSupportLoaderManager()。initLoader(FORECAST_LOADER,null,this)
  • 之前 onCreateLoader()

。 发生了什么事??。 我可以使用与LoaderCallbacks相同的LoaderID吗?

public class DetailActivity extends AppCompatActivity implements
    LoaderManager.LoaderCallbacks<Cursor> {

    private static final int FORECAST_LOADER = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        .................
        // Init Loader data from provider
        getSupportLoaderManager().initLoader(FORECAST_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Long routeId = 1234; 

        Uri routesUri = RouteContract.RouteEntry.buildRouteUri(routeId);
        return new CursorLoader(this,
            routesUri,
            RouteContract.RouteEntry.FORECAST_COLUMNS,
            null,
            null,
            null);
     }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        ..............(Run OK)
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        ............. (Run OK)
    }  

1 个答案:

答案 0 :(得分:0)

我找到了简单的解决方案:

  • init 活动(savedInstanceState == null): initLoader
  • 旋转设备后(savedInstanceState!= null): restartLoader

    if (savedInstanceState == null) {
        getSupportLoaderManager().initLoader(FORECAST_LOADER, null, this);
    } else {
        getSupportLoaderManager().restartLoader(FORECAST_LOADER, null, this);
    }