3

下面是我的代碼:savedInstanceState總是在片段空

public class MyListFragmentActivity extends FragmentActivity{ 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    System.out.println("DEBUG : MLFA onCreate"); 
    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragment()) 
      .replace(detailFragmentID, new MyDetailFragment()).commit(); 
     } 
    } 



    @Override 
    protected void onRestart() { 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      Fragment prevFrag = getSupportFragmentManager().findFragmentById(detailFragmentID); 
    if (prevFrag != null) { 
     fragmentTransaction.remove(prevFrag); 
     getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()).commitAllowingStateLoss(); 
    } else { 
     getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()).commitAllowingStateLoss(); 
    } 
} 

MyListFragment

public class MyListFragment extends Fragment{ 

//When we click on each item in list view call detail fragment to relad its layout 
OnItemClickListener onItemClickListener = new OnItemClickListener() { 

/** Getting the fragmenttransaction object, which can be used to add, remove or replace a fragment */ 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     /** Getting the existing detailed fragment object, if it already exists. 
     * The fragment object is retrieved by its tag name 
     * */ 
     Fragment prevFrag = getFragmentManager().findFragmentById(detailFragmentID); 

     /** Remove the existing detailed fragment object if it exists */ 
     if (prevFrag != null) { 
      fragmentTransaction.remove(prevFrag); 
      MyDetailFragment mydetailFragment = new MyDetailFragment(); 
      fragmentTransaction.replace(detailFragmentID, mydetailFragment); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.show(getFragmentManager().findFragmentById(detailFragmentID)); 
      fragmentTransaction.commit(); 
     } 
} 

MyDetailFragment

public class MyDetailFragment extends Fragment{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
setRetainInstance(true); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    if (savedInstanceState != null) { 
    // it is not entering the inside here 
} 

@Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
     // saving some values 
    } 

當我的標題我的設備後,我設置setRetainInstance(true); savedInstanceState始終爲空,那麼我如何才能在此保存我的值?

爲什麼這麼說?我在這裏做錯了什麼,以及如何解決這個問題?

回答

0

我認爲你失去了你的instanceState,因爲你一直在你的onRestart()方法中創建一個新的Fragment實例。

試試這樣說:

@Override 
protected void onRestart() { 
    Fragment prevFrag = getSupportFragmentManager().findFragmentById(detailFragmentID); 
    if (prevFrag == null || !(prevFrag instanceof MyDetailFragment)) { 
     getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()); 
    } 
} 

這樣,你只有唯一的,如果有相同類型的沒有其他有效的片段附上片段的新實例。

請注意setRetainInstance(true);防止FragmentManager在發生配置更改時銷燬您的Fragment實例。 因此,手動銷燬你的Fragment(通過調用.remove(...))是沒有意義的,然後用.replace(...,new MyDetailFragment())初始化一個新的。這就是爲什麼你總是得到一個空的savedInstanceState:你在一個新的實例,所以沒有以前保存的狀態!

還要記住,在FragmentTransaction上調用.commitAllowingStateLoss()可以讓片段管理器避免保存savedInstanceState,因此只有在您確實知道自己在做什麼時才應該使用它。

祝您有美好的一天! :)

0

根據安卓

的onSaveInstanceState()將默認爲如果視圖被調用,只有它有一個ID。

默認實現通過在具有ID的層次結構中的每個視圖上調用onSaveInstanceState(),並保存當前焦點視圖的ID來爲您處理大部分UI每個實例狀態(所有這些由onRestoreInstanceState(Bundle)的默認實現恢復)。

相關問題