Fragment ViewState restored in onStart?

前端 未结 3 1981
长发绾君心
长发绾君心 2021-02-06 05:08

When orientation changes, fragment viewState restored only in onStart. After onAttach, onCreateView, onViewCreated

3条回答
  •  无人及你
    2021-02-06 05:52

    In Android API >= 17 (Android 4.2 Jelly Beans) there is a method: public void onViewStateRestored (Bundle savedInstanceState)

    which is called before onStart() and after onActivityCreated() as mentioned in docs.

    In Android API < 17 there is no such method. But there are two solutions:

    1. Don't rely on view state while initializing Fragment and save all required initialization state as Fragment state (i.e. override Fragment#onSaveInstanceState()). Later you can restore fragment state in onCreate(), onCreateView() or onViewCreated().
    2. Perform initialization in onStart() as specified in question.

提交回复
热议问题