Android: Saving and Restoring data when switching

2019-01-26 23:05发布

问题:

I am trying to save some data in "MainActivity" when switching to another activity, and restoring that data as I switch back to it.

In "MainActivity": (restoring data)

            protected void onCreate(Bundle savedInstanceState) {
                   // do usual stuff
                   restoreData();
            }

In "MainActivity": (switching to "StatusActivity"):

            Bundle data = saveData();
            Log.d(TAG, "Sending data to status activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
            intent.putExtras(data);
            startActivity(intent);  

In "StatusActivity":

            Bundle data = getIntent().getExtras();
            Log.d(TAG, "Sending data to main activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtras(data);
            startActivity(intent);  

My saveData() function:

private Bundle saveData() {
    Log.d(TAG, "Started saving state");
    Bundle data = new Bundle();

    // store stuff in the bundle
    return data;
}

My restoreData() function:

private void restoreData() {
    Log.d(TAG, "Started restoring state");

    Bundle data = getIntent().getExtras();
            // restore stuff in the bundle
}

LogCat:

My bundle is fine when sending to StatusActivity:

Sending data to status activity intent: 
Bundle[{obj0=Bundle[{timeSinceLastPooped=3224, hunger=5, 
id=2130837505, timeSinceLastHungerUpdate=3224, 
timeSinceLastFed=0, timeSinceLastHappinessUpdate=3224, 
timeSinceLastEvolution=3224, posY=0.0, posX=0.0, 
isDead=false, happiness=5, evolutionStage=0, type=pet}], 
time=7.794168E7}]

But then sending back to MainActivity:

Sending data to main activity intent: Bundle[mParcelledData.dataSize=648]

What do I do with mParcelledData to get the original bundle back? Thanks!

Answer:

In "MainActivity": (restoring data)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if(requestCode == 1) {
        if(resultCode == RESULT_OK) {
            restoreData(intent);
        }
    }
}

In "MainActivity": (switching to "StatusActivity"):

            Bundle data = saveData();
            Log.d(TAG, "Sending data to status activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
            intent.putExtras(data);
            startActivityForResult(intent, 1);

In "StatusActivity":

            Bundle data = getIntent().getExtras();
            Log.d(TAG, "Sending data to main activity intent: " +data);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtras(data);
            setResult(RESULT_OK, getIntent());
            finish();   

回答1:

You can use onRestoreInstanceState() witch is called after onStart(), whereas onCreate() is called before onStart().

Use the put methods to store values in onSaveInstanceState():

protected void onSaveInstanceState(Bundle bundle) {
  super.onSaveInstanceState(bundle);
  bundle.putInt("value", value);
}

And restore the value in onCreate():

public void onCreate(Bundle bundle) {
  if (bundle!= null){
    value = bundle.getInt("value");
  }
}