2012-12-24 54 views
8

當方向更改時,片段viewState僅在onStart中恢復。 之後onAttach,onCreateView,onViewCreatedonActivityCreated並且甚至在onCreate之後。 爲什麼?這太晚了。在onStart中恢復片段ViewState?

我需要基於某些TextView值將數據庫查詢結果填充到ListView。目前我嘗試在onViewCreated中這樣做。但視圖狀態在此步驟中不會恢復。

我可以強制恢復嗎? 或者如何解決這個問題? 任何想法,請。

PS:我使用actionbarsherlock和從屬機器人支撐-V4 R7庫

PS2:如果我將在onStart加載數據然後當片段被恢復後onStop(ⅰ可以通過添加一些解決這個它會做附加查詢布爾型isLoaded - 但這不是最好的解決方案)。

回答

12

一個很好的例子在Android中API> = 17(的Android 4.2糖豆)有如下方法: public void onViewStateRestored (Bundle savedInstanceState)

onStart()之前調用和onActivityCreated()之後如在docs提及。

在Android API < 17中沒有這樣的方法。但是,有兩種解決方法:

  1. 不要依賴於視圖狀態,同時初始化Fragment並保存所有必需的初始化狀態Fragment狀態(即覆蓋Fragment#onSaveInstanceState())。稍後,您可以在onCreate()onCreateView()onViewCreated()中恢復片段狀態。
  2. 按照有關規定在onStart()中執行初始化。
4

[編輯1 - - - - - - - ]

//檢查,看看是否片段回棧已被填充

//如果沒有,創建並填充佈局。

//所以你不會片段重新

YourFragment yourFragment = (YourFragment)fm.findFragmentById(R.id.fragment_container); 

    if (yourFragment == null) { 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.replace(R.id.fragment_container, new YourFragment());  
     ft.commit(); 
    } 

[編輯1 - - - - - - - ]

enter image description here

/** 
* Listing 4-4: Fragment skeleton code 
* Listing 4-5: Fragment lifecycle event handlers 
*/ 
package com.paad.fragments; 

import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MySkeletonFragment extends Fragment { 

    // Called when the Fragment is attached to its parent Activity. 
    @Override 
    public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    // Get a reference to the parent Activity. 
    } 

    // Called to do the initial creation of the Fragment. 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Initialize the Fragment. 
    } 

    // Called once the Fragment has been created in order for it to 
    // create its user interface. 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
    // Create, or inflate the Fragment's UI, and return it. 
    // If this Fragment has no UI then return null. 
    return inflater.inflate(R.layout.my_fragment, container, false); 
    } 



    // Called once the parent Activity and the Fragment's UI have 
    // been created. 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    // Complete the Fragment initialization Ğ particularly anything 
    // that requires the parent Activity to be initialized or the 
    // Fragment's view to be fully inflated. 
    } 

    // Called at the start of the visible lifetime. 
    @Override 
    public void onStart(){ 
    super.onStart(); 
    // Apply any required UI change now that the Fragment is visible. 
    } 

    // Called at the start of the active lifetime. 
    @Override 
    public void onResume(){ 
    super.onResume(); 
    // Resume any paused UI updates, threads, or processes required 
    // by the Fragment but suspended when it became inactive. 
    } 

    // Called at the end of the active lifetime. 
    @Override 
    public void onPause(){ 
    // Suspend UI updates, threads, or CPU intensive processes 
    // that don't need to be updated when the Activity isn't 
    // the active foreground activity. 
    // Persist all edits or state changes 
    // as after this call the process is likely to be killed. 
    super.onPause(); 
    } 

    // Called to save UI state changes at the 
    // end of the active lifecycle. 
    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate, onCreateView, and 
    // onCreateView if the parent Activity is killed and restarted. 
    super.onSaveInstanceState(savedInstanceState); 
    } 

    // Called at the end of the visible lifetime. 
    @Override 
    public void onStop(){ 
    // Suspend remaining UI updates, threads, or processing 
    // that aren't required when the Fragment isn't visible. 
    super.onStop(); 
    } 

    // Called when the Fragment's View has been detached. 
    @Override 
    public void onDestroyView() { 
    // Clean up resources related to the View. 
    super.onDestroyView(); 
    } 

    // Called at the end of the full lifetime. 
    @Override 
    public void onDestroy(){ 
    // Clean up any resources including ending threads, 
    // closing database connections etc. 
    super.onDestroy(); 
    } 

    // Called when the Fragment has been detached from its parent Activity. 
    @Override 
    public void onDetach() { 
    super.onDetach(); 
    } 
} 

來源:專業的Android開發4 - Reto Meier

+0

in'onAttach'視圖不存在。 正如您在'onActivityCreated'視圖中的評論中所指定的那樣存在,但保存的片段視圖狀態尚未恢復。 – acc15

+0

是的view是在onCreateView上創建的。 – Talha

+0

從onCreateView返回的LayoutView未初始化幷包含初始值。當方向改變時,我需要恢復的值。 – acc15