我们怎样才能保持一个简单的状态阵列横跨配置更改使用无头的片段? [英] How can we retain a simple State Array across Configuration changes using Headless Fragments?

查看:129
本文介绍了我们怎样才能保持一个简单的状态阵列横跨配置更改使用无头的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了许多其他类似的问题了,但没有解决我的问题。
一个片段(显然)的用途是坚持的状态。
我有一个名为arrState一国阵,我裹在被称为StateFragment无头片段。

I have gone through so many other similar questions but none solve my problem. One of the uses of Fragments (apparently) is to persist a state. I have a State array called arrState that I have wrapped in a headless fragment called 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的活动填充与X的数组arrState。

I have an Activity called ActMainGame that fills the array arrState with Xs.

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();
}

我已经设置了登录这两个类的主要生命周期事件。
我想到的是,在这样做的主要活动得到重建,但能够找到片段的方向改变(withs它包含9 X的状态数组)。果然片段不被破坏,但活动不能找到通过线路的持续片段:

I have set up logging on the major lifecycle events of both classes. What I expect is that on doing an orientation change that the main activity gets rebuilt but is able to find the fragment (withs it state array containing 9 Xs). Sure enough the fragment is not destroyed but the activity cannot find the persisting fragment via the line:

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

和因此创建一个新片段。

And therefore creates a new 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

我清楚地意识到,还有其他方法坚持一个状态变量,但我想这样做了片段方式。

I am well aware that there are other ways to persist a state variable but I want to do it the "fragment way".

推荐答案

我终于解决了这个!这是Eclipse的一个重大BUG!
当您在Eclipse中创建一个新的Andr​​oid应用程序项目,该向导可以让你开始用动作条。如果你这样做,还可以选择导航类型操作栏微调向导默认除其他事项外创建以下code:

I finally resolved this! It is a MAJOR bug in Eclipse!! When you create a new Android Application Project in Eclipse, the wizard allows you to start with an actionbar. If you do this and also select Navigation Type "Action Bar Spinner" the wizard creates among other things the following code by default:

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

这是非常客气的说道向导,但存在着巨大的问题!还有就是超级没有呼叫!在code完美的作品以及在大多数情况下,但如果你想坚持一个片段使用SetRetainInstance(真)的活动,你需要在如下加上超级,或当你​​的活动再现它不会检测片段。

This is very kind of said wizard but there is a huge problem! There is no call to super!! The code works perfectly well in most circumstances BUT if you want to persist a fragment for your activity using SetRetainInstance(true) you need to add the "super" in as follows or it will not detect the fragment when your activity recreates.

应该是:

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

希望这样可以节省别人有天花费了我!

Hope this saves someone else the days it has cost me!

这篇关于我们怎样才能保持一个简单的状态阵列横跨配置更改使用无头的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆