屏幕方向更改时未调用加载程序

Mah*_*ver 8 android android-asynctask

使用时AsyncTaskLoader屏幕方向发生变化,我遇到了一些麻烦.让我给你一些关于我的应用程序结构的背景:我有一个非常简单的应用程序,它从一个URL获取结果并显示它.该应用程序由一个FragmentActivity和三个组成Fragments.我的三个片段如下:

  • 第一个片段是一个edittext和一个提交输入的按钮.
  • 第二个片段显示了加载微调器
  • 第三个片段在获取数据时替换加载片段以显示结果.

AsyncTaskLoader被用于从任一个内容提供者加载数据(如果它是高速缓存),或从网络上.

一切都很好,直到屏幕上的变化!我查看了输出,LoaderManager.enableDebugLogging(true) 似乎问题的根源是LoaderCallbacks.onLoadFinished在我的活动中没有被调用.

这是启动加载器的一段代码:

/* This function is defined in the "first" fragment in an interface and
 * implemented in the activity */ 
@Override
public void onFormSubmission(String word) {                     

    showLoadingFragment();

    if ( mWord == null || mWord.equals(word) ) {
        getSupportLoaderManager().initLoader(0, word, this);
    } else {
        getSupportLoaderManager().restartLoader(0, word, this);
    }

    // Store the word
    mWord = word;
}
Run Code Online (Sandbox Code Playgroud)

显示结果的代码:

@Override
public void onLoadFinished(Loader<Bundle> loader, Bundle data) {
    // Validation and other stuff

      ResultFragment fragment = new ResultFragment();

      // Add the fragment to the 'result_fragment_container' FrameLayout
      getSupportFragmentManager()
            .beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
            // Replace loading fragment with the result
            .replace(R.id.result_fragment_container, fragment)
            .commitAllowingStateLoss(); 
}
Run Code Online (Sandbox Code Playgroud)

尝试过但尝试失败的方法:

  • 设置setRetaininstance(true)片段.
  • android:configChanges="orientation|keyboardHidden|keyboard"在清单中设置.

我没有想法,所以任何帮助都会受到赞赏.

Qua*_*ium 1

你应该把方法getSupportLoaderManager().initLoader(0, null, this);放进去onActivityCreated 。如果加载器已经存在,initLoader 将创建或重新连接该加载器