In my app the activity which is currently running needs data from an activity which has started it. The parent activity has an object which method needs to be invoked to get the required data.
I am doing like this
In the ParentActivity
public static Puzzle puzzle; // Puzzle is a class
In the child activity
Puzzle puzzle = ParentActivity.puzzle;
ArrayList<String> cells = puzzle.getItemList();
It seems to work. But I am not sure about correctness of this approach. It allowed in android.
Will android run-time form destroying the parent activity.
If
Puzzleis some plain object representing a data model, what you have is “correct” so long as you consider this to only be a cache and ifPuzzleand its contents are fairly small.Your process can be terminated at any time after it leaves the foreground. At that point, your cached
Puzzlevanishes. Any data that needs to survive needs to be stored persistently: file, database,SharedPreferences, “the cloud”, whatever. Your staticPuzzlecan be a cache of that persistent data, but the real data model is the persistent store, not the cachedPuzzle.Also, static data members represent intentional memory leaks. Anything referenced by that static
Puzzlecannot be garbage collected. That works so long as thePuzzleis small. This is also why it is important forPuzzleto be an ordinaryObject, not something more complex (e.g., anActivity) — the more complex the object, the more memory you leak.