I'm wondering, where is a good place to create and hold objects in android activities? Always the activity highest in the hierarchy? Here's what I mean.
/* Let this be the main launcher activity */
activity1{
List someList // Edit: This should of course be public, my mistake.
}
/* The next activity is a child of activity1
* and can be started by activity 1
*/
activity2{
...
do_something(activity1.someList); // Does this always work?
...
}
/* The next activity has no parent and can be launched
* when the app receives an intent, for example a
* photo is shared to my app.
*/
activityX{
....
receive_intent(...);
do_something(activity1.someList) // This might work, when app is already running
}
See, my problem is I'm never sure where it's okay to place objects. In my example activity2
needs access to one of activity1's objects, which I never had any problems with. But does this case always work? Will the objects from a parent activity always stay in memory while the child activity is visible?
Can I somehow pass a reference to someList
from activity1
to activity2
and pretend that someList
has been instantiated in activity2
? Or is this not required?
activityX on the other hand clearly will create a nullpointer exception when the app (and thus activity1
) is not running in the background (or just not cached?).
Is there a document with guidelines for android programming covering things like this?