FragmentA 正确响应 onConfigurationChanged,但如果我在其上打开 FragmentB 并旋转设备,它会触发 onConfigurationChanged,并且还会触发 FragmentA 的 onConfigurationChanged。
我在 Activity 中有fragmentA,FragmentA 响应其中的onConfigurationChanged 并在横向模式下打开另一个 Activity ,如果单击FragmentA 列出其打开的详细信息 fragment ,我们将其称为fragmentB。 fragmentB 也有 onConfigurationChanged 方法并以横向模式打开另一个 Activity 。
FragmentA{
@Override
public void onClick(View v) {
//open FragmentB
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//landScape of FragmentA
//open any Activaty
}
}
FragmentB{
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//landScape of FragmentB
//open any Activaty
}
}
fragmentB 上的方向更改会导致打开两个 Activity。一个来自fragmentA 的 onConfigurationChanged,另一个来自fragmentA 的 onConfigurationChanged FragmentB。
最佳答案
尝试此方法来解决您的问题。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Your code here
} else {
// Your code here
}
}
关于java - Activity 中多个 fragment 的方向发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920087/