SercioSoydanov SercioSoydanov - 5 months ago 14
Android Question

Android App Install Script Mechanism / Method

Is there any mechanism and / or programming practice on android to execute one-time install / update scripts? It may seem a very basic question but a google search didn't help.

Say my app uses a database and there needs to be a table for constant values (such as country names, city names, etc.) App should create a database and write the constant values in it, but should do it only if app is freshly installed or updated.

Should I;

a) Put a function in MainActivity to check if database and data in it is in place

b) Put a function in MainActivity to write a small chunk of file with current software version, then check it in each run to see if it is a fresh install or an update (of course it will first check the file, then write it if not present)

c) Or is there any other defacto method for this to run in each install and / or update?

Answer

That depends on how you will install and what change in your database.

If you just generate a new Apk and run it, it will update and not remove the previously data, but if you remove the APP and install the new, the data will be removed.

Other thing you should consider is the version of the Database, if the installed version is lower than the needed in your new application.

The control of that depends on what are you using to create the database, for example, if you are creating with a SQLiteOpenHelper a method like this normally is used:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}

Edit: When which method is called ?

On code:

DbName = "teste.db"
DbVersion = 1

On device: none db

^ Will invoke onCreate.

On code:

DbName = "teste.db"
DbVersion = 2

On device:

 DbName = "teste.db"
 DbVersion = 1

^ Will invoke onUpgrade

On code:

DbName = "teste.db"
DbVersion = 1

On device:

 DbName = "teste.db"
 DbVersion = 2

^ Will invoke onDowngrade

On code:

DbName = "anotherName.db"
DbVersion = 1

On device:

 DbName = "teste.db"
 DbVersion = 1

^ Will invoke onCreate

Comments