The Blog

Android: How to execute some code only on first time the application is launched

12 Apr 12

It happens you need to execute some piece of code only on the first time the user start using your app.

Lets say you want to show a quick tutorial to the user just once β€” Indeed, when the user open your app for the first time.

Well, an easy solution would be to use a SharedPreferenceΒ to store the info we need to accomplish this “task”.

Below, a snippet with a simple method ( to be included inside your activity class ) that will “solve” the problem.

Comments

  • fatou

    Useful, thanks

  • Chris

    This works great ! Thanks

  • jason

    this dont work, you cant set null on a boolean dude

  • Concurrent Cacher

    You can’t set a primitive `boolean` to null, but you can set a `Boolean` object to null πŸ™‚

  • Epilef Ish’aaratan

    Hi! I’m trying to create a file when the app is first time opened, I’m not sure where to write that lines, must they be after editor.commit() ? Thanks!

  • Ahsin Irshad

    Thanks Brother
    and add following code to your code then it works fine
    else{
    firstTime = false;
    }

  • scorlin_gonzalez more

  • zafar hussain

    If you uninstall the application and when you install it again , this piece of code will run once again.. Using SP is not good idea …because after uninstall the application data in SP is completely clear…