Thursday 20 September 2012

First Run Preferences

When designing the application, at times, we come across a situation, that we need certain things to be handled when user is using the app for the first time. It may be a Disclaimer note we are showing up for the usage  data collection of app. It, even, can be a simple "Thank You" for downloading our app. :-)

SharedPreferences comes handy in such scenarios. SharedPreferences is key-value pair based persistent storage type. We can use this to store the value which gets updated only once. Each time application launches it will check for this value in preferences. If the value is set, then this is not the first run of the application, else it is.

Application life cycle is managed with the Application class of Android framework. We are going to use SharedPreferences to store the value of first run. We will use a boolean variable to test if it is first run in preferences. When the application is installed and used for the first time, there are no preferences available for it. This time it will be created with provided default values. As we are testing for first run, we are going to use true as default value. In such case, the flag will return true value. After getting true flag, we will update this flag with false value as first run case has been executed and we no longer need flag to be true.



Here is the code snippet for the same:

public class MyApp extends Application {

 SharedPreferences mPrefs;

 @Override
 public void onCreate() {
  super.onCreate();

  Context mContext = this.getApplicationContext();
                //0 = mode private. only this app can read these preferences
  mPrefs = mContext.getSharedPreferences("myAppPrefs", 0);

 
  // the rest of your app initialization code goes here
 }
 
 public boolean getFirstRun() {
  return mPrefs.getBoolean("firstRun", true);
 }

 public void setRunned() {
  SharedPreferences.Editor edit = mPrefs.edit();
  edit.putBoolean("firstRun", false);
  edit.commit();
 }
  
}
This flag from preferences will be tested in launcher activity like this:

 if(((MyApp) getApplication()).getFirstRun()){
  //This is first run
  ((MyApp) getApplication()).setRunned();

  // your code for first run goes here

  }
 else{
  // this is the case other than first run
 }
 
When we publish an update to the app and the user downloads it via the Market, this will not modify the preferences, so the code works for only first run after installation. Consequent updates to the app will not bring the code into the picture, unless the user has manually uninstalled and installed the app. The preferences can also get modified, if user goes to Settings and clears data for the particular app. Once user clear application data, preferences get deleted. In such cases, above code will as if it is the first run of the application, even though it is not.
If you need a per-version mechanism you can, of course, just add a version number to the name used as a key in the shared preferences.

Similar technique can be used for distributing shareware versions of an Android app. i.e., we can limit the number of trials of the application using similar technique. In that case, we would use the integer count value in preferences for number of trials. Each trial would update the preferences. After the desired value is reached, we would block the usage of application.

No comments:

Post a Comment