0

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..

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
  • 1
    Maybe a duplicate. http://stackoverflow.com/questions/7217578/android-apllication-first-run – sealz Sep 01 '11 at 11:25
  • I think this link would be best according to code http://stackoverflow.com/questions/8617662/android-how-can-i-know-if-it-is-the-first-time-the-application-launched – surhidamatya Nov 07 '13 at 03:43
  • Look here that code: http://stackoverflow.com/a/7217834/4039762 Enter this in your Activity – Androider123454321 Oct 10 '14 at 17:50

2 Answers2

6

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

Jayp
  • 782
  • 2
  • 6
  • 21
3

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.

Jayp
  • 782
  • 2
  • 6
  • 21
Egor
  • 39,695
  • 10
  • 113
  • 130