![](/img/trans.png)
[英]Orientation Change Causes Series of Fragments in Activity Disappear and Do Not Apply Save Instance State
[英]Orientation change of multiple fragments in activity?
FragmentA正確響應onConfigurationChanged,但如果我在其頂部打開FragmentB並旋轉設備,則它會觸發onConfigurationChanged並還會觸發FragmentA的onConfigurationChanged。
我在activity中有fragmentA,並且FragmentA在其中響應onConfigurationChanged,並在橫向模式下打開另一個活動,如果單擊FragmentA列表,則其打開的詳細信息片段將其稱為fragmentB。 fragmentB還具有onConfigurationChanged方法,並以橫向模式打開另一個活動。
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的方向更改導致打開兩個活動。一個活動來自fragmentA的onConfigurationChanged,另一個來自FragmentB的onConfigurationChanged。
嘗試使用此方法解決您的問題。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Your code here
} else {
// Your code here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.