在 activity 之上设置新片段不适用于 onConfiguration 更改

Setting new fragment on top of activity doesn't work with onConfiguration change

我有带有 activity 的应用程序并在其中嵌套了 Fragment

当我将 activity 方向锁定为纵向时,添加嵌套 Fragment 工作正常。
然后我想处理一些 onConfiguration 更改的事情,所以我从清单中删除了屏幕方向,并以编程方式锁定了 activity 并覆盖了 activity 中的 onConfiguration 更改。

但在这种情况下,我尝试在 activity 之上设置一个 Fragment

它的 onCreateView 被调用但没有出现在屏幕上。

我已经检查了此处描述的解决方案,但没有奏效。 link

这是我替换片段的代码

    protected void setFragment(Fragment fragment) {

    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction fragmentTransaction;
    fragmentTransaction =
            fragmentManager.beginTransaction();

    fragmentTransaction.isAddToBackStackAllowed();

    fragmentTransaction.replace(R.id.container, fragment);
    //  fragmentTransaction.addToBackStack("frag1");

    fragmentTransaction.commit();
}

protected void addFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.defff, R.anim.defff, R.anim.right_out);
    fragmentTransaction.isAddToBackStackAllowed();
    fragmentTransaction.add(R.id.container, fragment);
    fragmentTransaction.addToBackStack("frag2");
    fragmentTransaction.commit();
}

和 onConfiguration 更改

 @Override
public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);

    RTA.setLocale(getApplicationContext(), RTA.getSharePrefrenceLocale(getApplicationContext()));
    //System.out.println("device rotated");getWindowManager().getDefaultDisplay().getRotation();


    if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent Madinti = new Intent(this, Madinati.class);
        Madinti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Madinti.putExtra("side", 1);


        getApplicationContext().startActivity(Madinti);
    } else if (getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_90) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent Madinti = new Intent(this, Madinati.class);
        Madinti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Madinti.putExtra("side", 0);


        getApplicationContext().startActivity(Madinti);
    } else {

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

    }
}

和清单中的 activity

  <activity
        android:name=".Parking.click_add"
        android:configChanges="orientation|keyboardHidden|screenSize|locale"
        android:label="@string/title_activity_click_add"
         />

这里是第二个添加片段的onCreate和onAttach

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if (mRetainedChildFragmentManager != null) {
        //restore the last retained child fragment manager to the new
        //created fragment
        try {
            Field childFMField = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFMField.setAccessible(true);
            childFMField.set(this, mRetainedChildFragmentManager);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    } else {
        mRetainedChildFragmentManager = getChildFragmentManager();
    }
}  

任何帮助谢谢

调用setRetainInstance(true);在片段中的 OnActivityCreated 回调中。