1
我在我的應用程序中遇到問題,即使使用了一些最基本的代碼(請參見下文),我在循環中丟失片段視圖。旋轉丟失片段
一切都開始了偉大的:
直到你旋轉:
故事是相同的其他方式還有:
我不確定發生了什麼事。我試圖遵循各種教程和代碼示例,通過日誌記錄進行檢查以確保碎片正在保存和恢復,但無濟於事。我究竟做錯了什麼?
MainActivity.java
public class MainActivity extends FragmentActivity
{
private FragmentManager fragment_manager;
private CharacterMenuFragment characterMenuFragment;
private CharacterEditFragment characterEditFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame);
fragment_manager = getSupportFragmentManager();
if (savedInstanceState != null) {
characterMenuFragment = (CharacterMenuFragment) fragment_manager.getFragment(savedInstanceState, "characterMenuFragment");
characterEditFragment = (CharacterEditFragment) fragment_manager.getFragment(savedInstanceState, "characterEditFragment");
}
if (characterMenuFragment == null) {
Log.i("characterMenuFragment", "making new fragment");
characterMenuFragment = new CharacterMenuFragment();
} else {
Log.i("characterMenuFragment", "re-using old fragment");
}
if (characterEditFragment == null) {
Log.i("characterEditFragment", "making new fragment");
characterEditFragment = new CharacterEditFragment();
} else {
Log.i("characterEditFragment", "re-using old fragment");
}
fragment_manager.beginTransaction().replace(R.id.menu_frame, characterMenuFragment).commit();
fragment_manager.beginTransaction().replace(R.id.content_frame, characterEditFragment).commit();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
fragment_manager.putFragment(savedInstanceState, "characterMenuFragment", characterMenuFragment);
fragment_manager.putFragment(savedInstanceState, "characterEditFragment", characterEditFragment);
}
}
CharacterEditFragment.java
public class CharacterEditFragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hello_world, container, false);
return view;
}
}
CharacterMenuFragment.java
public class CharacterMenuFragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hello_world, container, false);
return view;
}
}
content_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/menu_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
hello_world.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
非常好,非常感謝你,沒有意識到恢復過程會讓他們回到它自己的。 –
如果我們按回來按鈕,方向改變後,我們會得到空白屏幕。 – kavie