0

I have some DB work that I need to be done once when the app is installed and ran the first time.

I know I need to use the SharedPreferences to track this to make sure it is done once. But my question is when this work should be done (or the methods that do that should be called). Is it done on the OnCreate() of the home page of the app (first screen) or is done in a derived class from the Application class?

Right now I am doing it in the application class, however the only down side is that the I can't display ProgressBar to indicate things are being worked on (probably because there is not Context attached yet). But I want to confirm that this is a correct place to do preliminary things in the first place

Thank you

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
Snake
  • 14,228
  • 27
  • 117
  • 250
  • 2
    You are going the right way.it should be done in the startup and there are no better place to do other than the Application class.Also you can have a baseActivity in your code if you want some UI operations.This will be created once only . All the other activities can extend this BaseActivity instead of directly extending Activity. – Ritaban Sep 02 '13 at 18:11

2 Answers2

0

I would do it in the onCreate() using AsyncTask which will perform it in the background and it can post back to the, what you call, "first screen" the progress, and you can display progress bar.

KickAss
  • 4,210
  • 8
  • 33
  • 41
  • My only concern that everytime this activity is created this code will be called (well it won't run because it is checking SharefPreferences) however the extra checking is redundant as opposed to Application which is initiated once. What do you think? – Snake Sep 02 '13 at 18:13
  • That is correct. And `Application class` is the best place to do it... However, I wouldn't worry too much about the Code running at each startup. Checking Shared preferences is 3 lines and takes up virtually no resources, and using an `if` statement, the entire block of code will be avoided after first run. So App start-up speed won't be affected that is noticeable to the human naked eye. Consider this: Is it really improtant to display `ProgressBar`? If not, just use `Application class`, if you must display it, use onCreate, it won't do any harm. – KickAss Sep 02 '13 at 18:20
  • Also. See this: http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – KickAss Sep 02 '13 at 18:22
  • Check my other question plz which I just posted.. sort of related to this http://stackoverflow.com/questions/18578983/upgrade-rows-on-app-upgrade-only – Snake Sep 02 '13 at 18:28
  • I haven't got much experience in DB's yet unfortunately. I'm actually a self-taught Java and Android novice developer lol. I'm fairly new to Android myself and am always reading tutorials. Can't help you on that second question :( Sorry buddy. – KickAss Sep 02 '13 at 18:31
  • Same here ...Same here :) Thanks though – Snake Sep 02 '13 at 18:36
0

try this

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
// run your one time code
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
khubaib
  • 535
  • 4
  • 12