tags:

views:

50

answers:

3

I've tried this and it works, but I didn't know if this was a bad thing or not, as all the help on data transfers between Activities seems to use intents.

In MainActivity I have:

static PilotRecord pilotRecord = new PilotRecord(); //PilotRecord just contains data item declarations

In MainActivity.onCreate:

pilotRecord.fuel = 100;

In MainActivity.onClick:

Intent intent = new Intent(this, SubActivity.class);
startActivityForResult(intent, 0);

In SubActivity.onCreate I have:

MainActivity.pilotRecord.fuel = 200;

In SubActivity.onClick:

MainActivity.pilotRecord.fuel = 300;
setResult(RESULT_OK);
finish();

When I start MainActivity, the fuel value is 100

If I click in MainActivity, SubActivity is displayed, as expected

If I click in SubActivity, MainActivity is displayed and the fuel value is now 300

If I press the Back button, MainActivity is displayed and the fuel value is now 200

Does anyone know of any potential issues with this as it seems simpler to me than setting up intents etc.

  • Frink
A: 

Why don't you put the result in the result intent ?

fedj
Because it's easier to just update the variable directly, isn't it?
FrinkTheBrave
If I read your question, no ;-)
fedj
+3  A: 

It is my understanding that what you are doing will result in data loss.

The SubActivity should not be manipulating the data of the activity that called it. I don't think that there is any guarantee that your MainActivity even exists...

The system may decide to kill it at any time and restart it when your SubActivity signals that it is ready to return to the MainActivity.

You should pass back the data in a bundle and let MainActivity modify its data based on the results.

    Bundle stats = new Bundle();
    stats.putString("fuel","300"); 
    setResult(RESULT_OK, "PilotRecord", stats);
    finish();

Also, remember that you should be saving the "Fuel" level to some sort of persistent storage when onPause() is called in your MainActivity.

I would suggest reading the documentation for Activity carefully since it is quite important to implement the correct callbacks.

Timothy Lee Russell
I thought there might be a catch as everyone refers me to intents. Thanks for your explanation.Presumably I can still refer to constants like MainActivity.MAX_FUEL in my SubActivity as these are resolved at compile time?If I save fuel to persistent storage onPause, do I need to restore it onResume, or will it already be there? Do I only need to restore it onCreate (which would only be run if the activity gets destroyed by the system)
FrinkTheBrave
Persistent user data should be saved in onPause() and restored in onResume() and onCreate(). It will pass you a bundle but you are responsible for populating your variables with the data in the bundle. Application state data, such as data they are entering in a text box should be saved in onSaveInstanceState() and restored in onRestoreInstanceState(). Otherwise, when they rotate the screen or get a phone call that temporary data will be lost when your app is sent to the background.
Timothy Lee Russell
+1  A: 

If you really want to hack it, create another class that hold all your static variables that needs to be shared.

Jack
I have the PilotRecord class which holds all the variables, but it is instantiated in MainActivity.I think I might pass the whole class in the intent to and from each SubActivity, therefore I can use common code as it's the same for every class
FrinkTheBrave