Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Setting First-Run Preferences

Author: Ashwini Shahapurkar
Published? true
FormatLanguage: WikiFormat

Problem:

You have an application which collects app usage data anonymously, so you need to make the user aware of this the first time they run the app.

Solution:

We can use the shared preferences as persistent storage 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.

Discussion:

We can mange application lifecyle by using the Application class of Android framework. We will use SharedPreferences as persistent storage to store the value of first run.

You will store a boolean flag 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. That will be created for us. In that case the flag will return true value. After getting true flag, we can update this flag with false value as we no longer need it to be true.

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
	}

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

Note: You can use a similar technique 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.

sazary 2013-02-02 15:34:59.447 i think you can't use this mechanism to publish trial apps; because if they uninstall and install apps, they can continue using