-1
我在Activity的onCreate()方法中在運行時附加了一個片段。我想了解片段上生命週期方法調用的順序。然而,行爲似乎與預期不符。我正在做一個屏幕旋轉來理解這一點。以下是屏幕旋轉後的日誌。在懷疑的地方把標記放在日誌中。 有人可以解釋這裏發生了什麼嗎?片段生命週期方法序列
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(tag, "OnCreate()");
if (savedInstanceState != null) {
Log.d(tag, "SaveInstanceState is NOT NULL");
}
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
mainFragment = new MainFragment();
fm.beginTransaction().add(R.id.fragment_container, mainFragment, "MainFragment").commit();
}
MainFragment﹕ OnDestroy
MainFragment﹕ OnDetach
MainActivity﹕ OnDestroy
MainFragment﹕ onAttach() <<< Getting called before Activity's onCreate()
MainActivity﹕ OnCreate()
MainActivity﹕ SaveInstanceState is NOT NULL
MainFragment﹕ onCreateView()
MainFragment﹕ SaveInstanceState is NOT NULL
MainFragment﹕ onAttach() <<<<<< onAttach() on Fragment called again
MainFragment﹕ onCreateView() <<<<<<< onCreateView() on Fragment again.