1 回答

TA貢獻2012條經驗 獲得超12個贊
當您調用 時,Fragments不為空super.onCreate()時會自動恢復其當前狀態。savedInstanceState
因此,如果您希望通過添加初始片段來進行一次設置,則應始終在其周圍加上一個if (savedInstanceState == null)檢查:
@Override
public void onCreate(Bundle savedInstanceState)
{
// I assume you accidentally left out these lines
super.onCreate(savedInstanceState);
setContentView(R.id.your_content_view);
if (savedInstanceState == null) {
FragmentA fragA = new FragmentA();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransation.replace(R.id.basic_frame, fragA);
fragmentTransaction.commit();
}
}
添加回答
舉報