I have a main activity with a number of buttons. Each button is starting another activity.
One of these activities (called MyPuzzlesActivitiy) has a member which has to be loaded from an internet server in order to work. (This member is a list of puzzles to be loaded from the internet)
This member’s right place is the activity.
Even so, I would like to load it before the user actually press the button which starts the activity, to save the time (of waiting to the response from internet server).
I thought about making this member static and start it from the main activity, but it seems a little ugly to me.
Does someone have a better idea to solve this issue?
Thanks..
You’re correct, static is ugly in this case.
You can store it in the Application: first, you need to subclass
Applicationwith a custom class you design (class MyApplication extends Application), then declare it in the manifest (application name="...package...MyApplication").Then you can use it from any activity to store data, and retrieve it when you want. As long as your app is active, your data will be available. So you can load the list of your puzzles from your main activity, store it in the application (just add a setter and getter in
MyApplicationfor the type of data you want to store), and retrieve it from theonCreateof your other activity.And from your main activity:
and in
MyPuzzlesActivity: