Does onCreate in Application run on upgrading?

2020-07-27 04:57发布

问题:

I want to make some one-off mods to my application during the app upgrade. Checking the android.app.Application docs, I noticed there is onCreate but no onUpgrade (like in the android.database.sqlite.SQLiteOpenHelper).

I have put my upgrade code in the onCreate method but its not clear if the code is being executed during the upgrade.

Is onCreated called during an upgrade? Or is there another way of running upgrade specific code?

回答1:

Is onCreated called during an upgrade?

no. Application.onCreate() called when your application turning on from state it considers "not running" to "running"

application considered running when it's package have one or more active process. it will appear in the running applications list (not all firmwares can show them, but there's lot's of applications showing what packages running, and there is API to get this list) and probably have some running services or activities, but not necessarily! the system can keep your application in "running" state as long as it thinks it nesasery, and will kill it when it need to re-claim some memory, or if you explicitly killed it process somehow (there is also API to that, but no reason usually to use it).

is there another way of running upgrade specific code?

as @Class Stacker wrote: if you are modifying the version name or number when you realising new version - you can check on your main activity when it creates via the Activity.onCreate() method the version name, and compare it to the last version name you saved to sharedPreference or database table. then you can implement whatever logic you'd like when you recognize your version is changed.

you can check the version name and number with this code:

     PackageInfo pinfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
     Integer versionCode = pinfo.versionCode; 
     String versionName = pinfo.versionName; 

hope it helped you, and good luck.



回答2:

There are Broadcasts which Android sends upon package operations. However, the package in question is either excluded from receiving those or the delivery is not reliable (I do not recall the exact circumstances at the moment). So there's nothing you can rely on.

If you need an upgrade modification, you can use a shared preference file where you identify the installation and access that in Application#onCreate(). If the last version of your app used a file, too, then it will still be there unless the user deleted all app data.

Hence, it's all a bit use case specific, because it's not always obvious what an upgrade really is.

Note that you can have a use case specific shared preferences file in which only your upgrade information is included.

Don't let the name fool you; the file is private to your app if you want it to be (unless someone rooted the device, in which case all bets are off).



回答3:

Not sure what you're exactly trying to update, but you can check your current build number against one you save in per example SharedPreferences.

You can read more about that here; How can I check the system version of Android?



回答4:

There is "ACTION_MY_PACKAGE_REPLACED" broadcast: https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED You can use it to track app upgrade event. It's useful if you want to do something immediately after update, e.g. restart service.

But if you don't need to run your code immediately, it's better to check if version number is changed when you app is started. Just store previous version number somewhere and compare it.



回答5:

There is no callbacks wich runs during or right after updates.