1

I want to generate a unique ID for my application instance once it is installed. Then I want this to ID to be stored in SharedPreferences so it can be referred to in future.

So the function will look like:

val uniqueID = UUID.randomUUID().toString()

then I would save it to SharedPreferences.

How do I fire this function only once the application is installed (never to be fired again)?

NOTE: My app is being written in Kotlin

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • for unique id, visit https://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id . For checking first run, use sharedpreferences with key "isFirstRun" and default value true. Then, when checking the default value you have a true value, save false to it. Done. – ישו אוהב אותך Feb 16 '19 at 06:48

1 Answers1

0

Simply check if your shared preference id key returns you any data. If not, it has never been run. Otherwise, it has!

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
        if(sharedPreferences.getString("ID", null) == null) {
            sharedPreferences.edit().putString("ID", UUID.randomUUID().toString()).apply()
        }
TomH
  • 2,581
  • 1
  • 15
  • 30