Reputation: 773
I'm trying to load data from a webserver (getting a JSONArray back) during the start of my application.
So far I was able to get the array back only from the main activity with the following commands:
if (DEVELOPER_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());<br>
}
jsonArray = JsonParser.getJSONFromURL("cities2.php");
That gave me all the cities (or whatever data i needed) in the current class, but this is not a clean way to do it..
My Problem now is, how to get the cities from my SplashScreen Activity to the Activity where i need the data.
My goal is to load all the data from the webserver during the SplashScreen (about 4500 lines of text -> approx. 50KB data) and use them in the different Activities..
So, my main problem at the moment is how to get the data from the webserver that i load during the SplashScreen onto the "last" activity (i.e. SplashScreen -> MainActivity -> SetFilterActivity -> ShowDataActivity (here i need the data) )
I was reading here; Android SplashScreen
Here: How to make a splash screen (screen visible when app starts)?
Here: http://www.androidpeople.com/android-loading-welcome-splash-spash-screen-example
And here: http://www.41post.com/4588/programming/android-coding-a-loading-screen-part-1
And others.... :-)
I tried also to write a sperate class that extends Application and "store" the data in there (getters and setters), but somehow it does not work like i want.
Can anyone guide me into the right direction on how i can do that a proper way..
Thank you for your help
PS: I could easily load the data on each screen whenever i need it, but i want to load it at one point and just use it afterwards in the programm whenever and wherever i need it
Upvotes: 3
Views: 10425
Reputation: 75629
Use Database to keep and put your data there. Database is not bond to activity at all so once you store your data there, you can retrieve it later from everywhere and basically that's what database is here for - to keep your data :) Suggestion to use shared preferences is not really good, as it's basically key value storage. If you need to do any sorting, or conditional data fetch then you will find yourself reinventing the wheel.
Upvotes: 0
Reputation: 1549
i was having the same problem so instead of creating a new activity to show the splash screen i used a Dialog, so you won't need to share the data between the activities. The link i used to do that: http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
Upvotes: 3
Reputation: 10673
Load your accumulated data as shared preferences. You can then access them as typed key/value pairs from anywhere in your application.
See: http://developer.android.com/guide/topics/ui/settings.html
// Writing prefs
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(defaultInt, currentInt);
editor.putFloat(defaultFloat, currentFloat);
editor.putString(defaultString, currentString);
// Reading prefs
currentString = this.getResources().getString("my_string_value");
currentFloat = this.getResources().getFloat("my_float_value");
Using SharedPreferences results in much less overhead than extending Application -- it's going to be both faster and easier to maintain, particularly for 50kb of data, which is a fairly trivial amount.
Upvotes: 0