簡體   English   中英

我們如何使用無頭片段在配置更改中保留簡單的狀態數組?

[英]How can we retain a simple State Array across Configuration changes using Headless Fragments?

我經歷了很多其他類似的問題,但是都無法解決我的問題。 片段的用途之一(顯然)是保持狀態。 我有一個名為arrState的State數組,該數組已包裝在一個名為StateFragment的無頭片段中。

public class StateFragment extends Fragment {

public static ArrayList<Character> arrState;
protected ActMainGame mActivity = null;
private Character crtX;
public static final String TAG = "StateFragment";

@Override
public void onAttach(Activity activity) {
    Log.d(StateFragment.TAG, "StateFragment: onAttach");
    super.onAttach(activity);
    mActivity = (ActMainGame) activity;
}

@Override
public void onCreate(Bundle b) {
    Log.d(StateFragment.TAG, "StateFragment: onCreate");
    super.onCreate(b);
    setRetainInstance(true);
}

public void setToX() {
arrState = new ArrayList<Character>();
for (int i = 0; i < 9; i++) {
    arrState.add(crtX);
    }
}

我有一個稱為ActMainGame的Activity,它用Xs填充數組arrState。

public class ActMainGame extends Activity {

// Fragments
private StateFragment mStateFragment = null;
private static final String TAG_FRAGMENT = "state_fragment";


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(StateFragment.TAG, "ActMainGame: onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_game);

         // Add Headless Fragment (if not already retained)
    FragmentManager FM = getFragmentManager();
    mStateFragment = (StateFragment) FM.findFragmentByTag(TAG_FRAGMENT); 
        if (mStateFragment == null) {
        Log.d(StateFragment.TAG, "++ Existing fragment not found. ++");
        mStateFragment = new StateFragment();
            FM.beginTransaction().add(mStateFragment, TAG_FRAGMENT).commit();
            } else {
               Log.d(StateFragment.TAG, "++ Existing fragment found. ++");
        }           
}

@Override
public void onStart() {
    super.onStart();
    if (D) Log.d(StateFragment.TAG, "ActMainGame: ON START");

    mStateFragment.setToX();
}

我已經設置了兩個類的主要生命周期事件的日志記錄。 我期望的是,在進行方向更改時,主要活動得以重建,但能夠找到該片段(包含9個X的狀態數組)。 果然片段未被破壞,但活動無法通過以下行找到持久的片段:

mStateFragment = (StateFragment) FM.findFragmentByTag(TAG_FRAGMENT); 

因此,創建了一個新的片段。

這是調試的結果:

++ Existing fragment not found. ++
StateFragment: onAttach
StateFragment: onCreate
ActMainGame: ON START
ActMainGame: onResume
<Orientation change done here>
ActMainGame: onPause
ActMainGame: onStop
StateFragment: onDetach
ActMainGame: onDestroy
ActMainGame: onCreate
++ Existing fragment not found. ++
StateFragment: onAttach
StateFragment: onCreate
ActMainGame: ON START
ActMainGame: onResume

我很清楚,還有其他方法可以保存狀態變量,但我想采用“分段方式”。

我終於解決了! 這是Eclipse中的主要錯誤!! 在Eclipse中創建新的Android應用程序項目時,該向導允許您從操作欄開始。 如果您執行此操作,並且還選擇導航類型“操作欄微調器”,則向導會默認創建以下代碼:

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current dropdown position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

向導說的非常好,但是有很大的問題! 沒有超級電話! 如果您想使用SetRetainInstance(true)為活動保留片段,則代碼在大多數情況下都可以很好地工作,但是您需要按如下方式添加“ super”,否則在重新創建活動時它將無法檢測到片段。

應該:

@Override
public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
    // Serialize the current dropdown position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

希望這可以節省別人花費我的時間!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM