-4

I was browsing through the below answer on Shared Preferences - Singleton class . Shared Preferences Singleton class

I read this two lines .

private  static final String MY_APP_PREFERENCES = "ca7eed88-2409-4de7-b529-52598af76734";
    private static final String PREF_USER_LEARNED_DRAWER = "963dfbb5-5f25-4fa9-9a9e-6766bfebfda8";

How to create those long String "ca7eed88-2409-4de7-b529-52598af76734" in java Android Studio . Its not typed .

What is its significance ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yatin
  • 2,969
  • 9
  • 34
  • 68
  • 2
    What do you mean is not typed? It's a UUID, stored in a String. The sixteen octets of a UUID are represented as 32 hexadecimal (base 16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens - which can be stored in a String just fine. – nbokmans Sep 18 '18 at 10:30

2 Answers2

1

You can use the UUID class from Java util package (java.util.UUID) use it like :

String value = UUID.randomUUID().toString()

It is used to generate a universally unique identifier for object id's, generally to be stored in a database and used as a primary key or a unique identifier for that object instance.

Aman J
  • 452
  • 4
  • 15
1

These are just keys for your shared preferences. They can be anything you want. In this case they have used UUIDs but you could just as easily have used something like the following:

private  static final String MY_APP_PREFERENCES = "my_app_preferences";
private static final String PREF_USER_LEARNED_DRAWER = "user_learned_draw";

The only requirement is they need to be unique. So only one SharedPreferences with the name "my_app_preferences" and inside there, only one preference with the key "user_learned_draw"

StuStirling
  • 15,601
  • 23
  • 93
  • 150