3

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

Dan Stef
  • 753
  • 1
  • 10
  • 25

3 Answers3

3

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/

Alan
  • 1,509
  • 1
  • 16
  • 21
  • ANother option is to use two different views. Load one view in onCreate, and then load another view when its done. But this is absolutely right- splash screens used to cover data loading should NOT be its own activity. – Gabe Sechan Apr 22 '13 at 20:48
  • Hi @Alan, thanks for the link.. I think i will use the description to solve this problem. – Dan Stef Apr 23 '13 at 17:25
  • Thank u @Alan its logical. – user3307005 Aug 23 '15 at 01:15
0

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.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • Thanks MarsAtomic, i will use the shared preferences for another problem that i am working on :-). For now i will use the solution from @Alan. Thanks for your help. – Dan Stef Apr 23 '13 at 17:28
0

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • A Database seems to be a lot of work for just 50kb of data.. And i dont want to store the data permanently.. Some data will change every few days and therefore they need to be loaded from the webserver every time the applicatioin starts.. Thank you for your help, though! – Dan Stef Apr 23 '13 at 17:26
  • No it is not a lot of work. It's easy and the onyl reason you do not like the idea is the fact you are not familiar with it. But it's simple. Ready any db tutorial and then do your app the right way – Marcin Orlowski Apr 23 '13 at 20:16