0

I have a activity that merely holds up a fragment, and this fragment has 2 objects that inherits from LinearLayout. I had a problem to manage the screen rotation, but i solved it up restoring and saving the state of these objects on their own onRestore and onSavedInstance methods. But this does't help when the activity that hold the fragment is destroyed. How can i proceed?

My activity

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

}

The fragment:

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

The methods i used to save and recover on the object

 @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);
}
Myrium
  • 205
  • 3
  • 11

1 Answers1

0

Did you consider using SQLight? You may save and load the objects to the sql of your phone and reload them on your activity reopening.

If you want to try this solution follow this link for sql tutorial: http://developer.android.com/training/basics/data-storage/databases.html

Andrey Dobrikov
  • 457
  • 4
  • 20
  • I do use already a database for another thing, if i create another one, would be to hold up only 6 variables D: – Myrium Jan 28 '16 at 14:56
  • It is great that you already have a database, just add a new table to hold those values, and use same database :) – Andrey Dobrikov Jan 28 '16 at 15:02