我现在有 2 个片段,一个片段处理纵向模式,然后另一个处理横向模式。但问题是当从纵向旋转到横向然后再旋转回纵向时。它不会显示在第一个纵向模式上显示的相同内容。有没有可以解决这个问题的代码?
此代码位于片段持有者内:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_holder);
FragmentManager fm = getSupportFragmentManager();
final Fragment fragment = Frag.newInstance(); //Portrait layout
final Fragment fragment2 = Frag2.newInstance(); //Landscape layout
int orientation = getResources().getConfiguration().orientation; //check whether is it portrait or landscape
if(orientation == Configuration.ORIENTATION_PORTRAIT){
Fragment fragTAG = fm.findFragmentByTag(TAG_P);
if(fragTAG == null){
Log.i("test","test");
fm.beginTransaction()
.replace(R.id.fragPlaceHolder, fragment, TAG_P)
.commit(); //Portrait
}
else{
fm.beginTransaction().replace(R.id.fragPlaceHolder,fragTAG).commit();
}
}
if(orientation == Configuration.ORIENTATION_LANDSCAPE){
Fragment fragTAG = fm.findFragmentByTag(TAG_L);
if(fragTAG == null){
fm.beginTransaction()
.replace(R.id.fragPlaceHolder, fragment2, TAG_L)
.commit(); //Landscape
} …Run Code Online (Sandbox Code Playgroud)