1

我已经按照这些步骤创建了一个ViewPager,但是当设备配置更改(例如,设备轮换)时,我遇到了加载程序重新启动的问题。

这不仅会导致不必要的查询,而且还会通过将用户带回到第一个项目(即,离开他们在旋转设备之前查看的项目)来重置 UI。

我试过调用我的类方法setRetainInstance(true)返回的片段,但没有帮助。getItem()ScreenSlidePagerAdapter

这里有一个类似的问题,但我期待/希望有更好的解决方案?

4

1 回答 1

0

这篇文章(特别是这部分)的帮助下,我找到了解决方案。

首先,我没有打电话...

getLoaderManager().initLoader(MY_LOADER_ID, null, this);

...在我的活动onCreate方法中。但是,如果我打电话,我只会实现正确的行为......

getLoaderManager().restartLoader(MY_LOADER_ID, args, this);

...仅在一次运行活动时。这是阻止执行新查询的唯一方法。

所以,这是我的活动方法的片段(我在创建andonCreate之后立即放置)......ViewPagerScreenSlidePagerAdapter

getLoaderManager().initLoader(LOADER_ID_NORMAL, null, this);

boolean isFirstCreate = (savedInstanceState == null);
if (isFirstCreate) {

    /*
     * If this Activity was launched with a search intent, then perform the search
     */
    parseIntent(getIntent()); //calls getLoaderManager().restartLoader()
}
else {

    /*
     * Hide the progress view
     */
    searchProgressBar.setVisibility(View.INVISIBLE);
}

对我来说可以正常工作,并且似乎是一个相当干净的解决方案 - 但任何其他建议都值得赞赏。

更新:2015 年 8 月 2 日

根据我的评论,我认为这是正确的做法:

if (getLoaderManager().getLoader(LOADER_ID_NORMAL) == null) {

    /*
     * Loader doesn't exist, so create and start it
     */ 
    Bundle args = ...;
    getLoaderManager().restartLoader(LOADER_ID_NORMAL, args, this); //calls onCreateLoader()
}
else {

    /*
     * Loader already exists so all we need to do is initialise it
     */
    getLoaderManager().initLoader(LOADER_ID_NORMAL, null, this); //calls onCreateLoader()
}
于 2014-12-27T12:38:27.083 回答