Is this the first run?


SDK Version: 
M3

Ever wanted to have a different flow of actions on the second or third run of your app? I had that many times, in almost every project that I haver worked on. So here is a little snippet, that you can use to store, and check the fact, if this is the first run of your app. You can modify it easily, to suit your needs.

The easiest way to do this, is to use SharedPreferences. It can be slow, if you store a lot of data in it, so don't treat it like a database, or a file. Please note that it won't be deleted, when you update a program.

  1. /**
  2.  * get if this is the first run
  3.  *
  4.  * @return returns true, if this is the first run
  5.  */
  6.     public boolean getFirstRun() {
  7.     return mPrefs.getBoolean("firstRun", true);
  8.  }
  9.  
  10.  /**
  11.  * store the first run
  12.  */
  13.  public void setRunned() {
  14.     SharedPreferences.Editor edit = mPrefs.edit();
  15.     edit.putBoolean("firstRun", false);
  16.     edit.commit();
  17.  }
  18.  
  19.  SharedPreferences mPrefs;
  20.  
  21.  /**
  22.  * setting up preferences storage
  23.  */
  24.  public void firstRunPreferences() {
  25.     Context mContext = this.getApplicationContext();
  26.     mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
  27.  }

Just put the firstRunPreferences() in your onCreate, put a big if(getFirstRun()){} where you need it (for example: before warning screen), and inside that, put setRunned(); .