-1

I have an if condition in my Java code in Android and I want to enter the block it's the first time the user has run the app. But for the second time and thereafter, they ignore the conditions.

How can I restrict the if block in my Java code?

This is my if block condition:

if(AppSharedPrefs.getInstance(SplashActivity.this)
                 .readPrefs‌​(SplashActivity.this‌​, "language").isEmpty())
{
    AppSharedPrefs.getInstance(SplashActivity.this)
                  .writePrefs(S‌​plashActivity.this, "language","English");
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hr intern
  • 23
  • 5

2 Answers2

3

You need to have shared preference for your purpose. On first time store value true to shared preference and when next time user comes to the app get value from shared preference if that is true you dont need to call your code

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
2

"Simple": you pick one of the options for storing data on the device.

And then, your code reads that data back every time the application starts.

See here for an overview of options, and there for the option you should pick (shared preferences).

In other words: the whole idea is that you have to enable yourself to store data; so that your code can later make decisions based on that data. That is all there is to this.

GhostCat
  • 137,827
  • 25
  • 176
  • 248