Gabrielle
Gabrielle

Reputation: 4981

How to know if an app runs for the first time on device?

Can anyone tell me how can I know if an app runs for the first time on an device? In my app I need to receive some data from a server only when app runs for the first time..

Upvotes: 0

Views: 1589

Answers (2)

Egor
Egor

Reputation: 40193

Agree with Jayp's answer, just want to make it a bit more clear. When you try to get a value from SharedPreferences, there are two scenarios:

  1. Value indeed is there under the mentioned key and it's being returned.
  2. Value is not created yet and a default value is returned.

You can try to get value, and if it's not created yet - means your app is being run for the first time. Then you can put a value under this key and next time when you try to get the value - you'll receive the value you've put there. Hope this helps.

Upvotes: 3

Jayp
Jayp

Reputation: 822

Boolean flag to SharedPreferences or check if that data already exists in the system and retrieve only if needed.

Upvotes: 6

Related Questions