0

我有一個活動,只是舉起一個片段,而這個片段有2個從LinearLayout繼承的對象。我有一個問題來管理屏幕旋轉,但我解決了它恢復和保存這些對象的狀態onRestore和onSavedInstance方法。但是,當保存片段的活動被破壞時,這並沒有幫助。我該如何繼續?如何在活動被銷燬時從LinearLayour存儲對象?

我的活動

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

回答

0

你考慮使用對象的恢復方法SQ光?您可以將對象保存並加載到手機的sql中,並在您的活動重新打開時重新加載它們。

如果你想嘗試這種解決方案請點擊此鏈接爲SQL教程:http://developer.android.com/training/basics/data-storage/databases.html

+0

我使用已經數據庫爲另一件事,如果我創造一個又一個,將托起只有6個變量d: – Myrium

+0

它很好,你已經擁有一個數據庫,只需添加一個新表來保存這些值,並使用相同的數據庫:) –