当活动被销毁时,如何从LinearLayour存储对象?

时间:2016-01-28 14:34:32

标签: android android-fragments android-custom-view android-savedstate

我有一个只包含片段的活动,这个片段有两个继承自LinearLayout的对象。我有一个问题来管理屏幕旋转,但我解决了它恢复和保存这些对象的状态在他们自己的onRestore和onSavedInstance方法。但是当保存片段的活动被破坏时,这没有任何帮助。我该怎么办?

我的活动

public class LifeCounterActivity extends SingleFragmentActivity{
private String[] navMenuTitles;
private TypedArray navMenuIcons;
static String sFragmentTag;
private static String TAG = "LifeCounterActivity";
private static String KEY = "LifeCounterFragement";

LifeCounterFragment mFragment;

public static Intent newIntent(Context packageContext, String fragmentTag){
    Intent intent = new Intent(packageContext,LifeCounterActivity.class);
    intent.putExtra(KEY_ID_FRAGMENT, fragmentTag);
    sFragmentTag = fragmentTag;
    return intent;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (savedInstanceState != null){
        mFragment = (LifeCounterFragment) savedInstanceState.getSerializable(KEY);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,mFragment).commit();
    }else{
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = createFragment();
        fm.beginTransaction().add(R.id.fragment_container,fragment).commit();
    }

    /////////////////////////////////////////////////////////////////////////////////
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load titles from strings.xml
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);//load icons from strings.xml
    set(navMenuTitles, navMenuIcons);
    navMenuIcons.recycle();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putSerializable(KEY,mFragment);
    super.onSaveInstanceState(outState);
}

@Override
protected Fragment createFragment() {
    if (mFragment == null){
        mFragment = new LifeCounterFragment();
    }
    return mFragment;
}

}

片段:

    public class LifeCounterFragment extends Fragment implements LifeCounter.EventHistoryRecorder,Serializable{
    public static String TAG = "LifeCounterFragment";
    private static String KEY_PLAYER_1 = "Player1";
    private static String KEY_PLAYER_2 = "Player2";
    private static String KEY_HISTORY = "History";
    LifeCounter mFirstPlayer;
    LifeCounter mSecondPlayer;
    HistoryManager mHistoryManager;

    private static float ROTATION_COUNT = 180f;
    public LifeCounterFragment(){
        setArguments(new Bundle());
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Bundle b = new Bundle();
        b.putSerializable(KEY_HISTORY, mHistoryManager);
        getArguments().putAll(b);
    }



 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_life_counter, container, false);

        savedInstanceState = getArguments();
        if (savedInstanceState != null && savedInstanceState.size() > 0){
            mHistoryManager = (HistoryManager) savedInstanceState.getSerializable(KEY_HISTORY);
            mFirstPlayer = (LifeCounter) savedInstanceState.getSerializable(KEY_PLAYER_1);
            mSecondPlayer = (LifeCounter) savedInstanceState.getSerializable(KEY_PLAYER_2);
        }

        if (mHistoryManager == null) mHistoryManager = new HistoryManager();
        if (mFirstPlayer == null) mFirstPlayer = (LifeCounter) v.findViewById(R.id.player_1_counter);
        if (mSecondPlayer == null) mSecondPlayer = (LifeCounter) v.findViewById(R.id.player_2_counter);

我用来保存和恢复对象的方法

 @Override
public Parcelable onSaveInstanceState()
{
    Bundle bundle = new Bundle();
    bundle.putParcelable("superState", super.onSaveInstanceState());
    bundle.putInt(KEY_LIFE, this.mLife);
    bundle.putString(KEY_NAME, this.mTextViewPlayerName.getText().toString());
    bundle.putInt(KEY_LASTLIFE, this.lastLifeAmount);
    bundle.putSerializable(KEY_LISTENER, (Serializable) mListener);
    bundle.putBoolean(KEY_ASTRAL,this.isAstral);
    return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state)
{
    if (state instanceof Bundle) // implicit null check
    {
        Bundle bundle = (Bundle) state;
        this.mLife = bundle.getInt(KEY_LIFE); // ... load stuff
        this.lastLifeAmount = bundle.getInt(KEY_LASTLIFE);
        this.mListener = (EventHistoryRecorder) bundle.getSerializable(KEY_LISTENER);
        this.isAstral = bundle.getBoolean(KEY_ASTRAL);
        state = bundle.getParcelable("superState");

        recoverData(bundle.getString(KEY_NAME));
    }
    super.onRestoreInstanceState(state);
}

1 个答案:

答案 0 :(得分:0)

你考虑过使用SQLight吗?您可以将对象保存并加载到手机的sql中,然后在重新打开活动时重新加载它们。

如果您想尝试此解决方案,请点击此链接获取sql教程:http://developer.android.com/training/basics/data-storage/databases.html