锁定方向直到函数被执行以解决片段转换提交错误

lock orientation till a function get executed in order to resolve fragment transision commit error

下面尝试理解和实现。 在 MainActivity 中,调用异步任务来下载和呈现 webView 片段。在 onPostExecution() 中,我调用下面的函数来负责加载片段,如下所示。 onPostExecute() 中的 SelectionItem() 始终以位置 0 调用。为简单起见,下面的代码显示位置 0。但是 SelectItem(position) 具有切换大小写以创建不同的片段。

     public void SelectItem(position) {

    Fragment fragment = null;
    Bundle args = new Bundle();
    fragment = new FragmentFullPage();
    args.putInt(FragmentFullPage.ARG_SECTION_NUMBER, position + 1);
    args.putString("URLSTRING", urlString);
    fragment.setArguments(args);
    FragmentManager frgManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = frgManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, fragment);
    fragmentTransaction.commit();
}

在调用 asynctask 之前,我正在初始化一个变量 handleorientation = true 并在异步任务完成后将其重置为 false

如下实现 onConfigurationChange

         @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      if(handleOrientation == true) {
          if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
              Toast.makeText(this, "Keeping Portrait", Toast.LENGTH_SHORT).show();
          } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              Toast.makeText(this, "Keeping Landscape", Toast.LENGTH_SHORT).show();
          }
      }
  }

并在清单文件中处理 android:configChanges="orientation"。

但是当 运行 屏幕旋转并低于错误时仍然如此。

    08-15 15:43:16.657  26720-26720/com.srids.sathyasaiinspires E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1383)
        at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636)
        at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615)
        at com.srids.sathyasaiinspires.MainActivity.SelectItem(MainActivity.java:224)
        at com.srids.sathyasaiinspires.MainActivity$HandleWebPageLoad.onPostExecute(MainActivity.java:160)
        at com.srids.sathyasaiinspires.MainActivity$HandleWebPageLoad.onPostExecute(MainActivity.java:136)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access0(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

其中 MainActivity.SelectItem(MainActivity.java:224) 是 fragmentTransaction.commit();

为什么不在启动 AsyncTask 之前询问当前屏幕方向并将其锁定到 onPostExecute()?在启动后台线程之前尝试这样的事情:

if(getActivity().getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}