Run a piece of code only once when an application is installed

39,531

Solution 1

Before all you can use SQLiteOpenHelper. It is preferred way to do things with database. This class have a onCreate(SQLiteDatabase) method, that called when first creating database. I think it suits you well.

If you want more flexibility and your first time logic is not tied only with database, you can use sample provided earlier. You just need to put it in startup spot.

There are 2 startup spots. If you have only single activity, you can put your code in onCreate method, so it will be like this:

public void onCreate(Bundle savedInstanceState) {
  // don't forget to call super method.
  super.onCreate(savedInstanceState);

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  if (!prefs.getBoolean("firstTime", false)) {
    // <---- run your one time code here
    databaseSetup();

    // mark first time has ran.
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
  }
}

Don't forget to put activity declaration in manifest, as well as it's intentfilters (action = MAIN, category = LAUNCHER).

If you have more than one activity and you don't want to duplicate your startup logic you can just put your initialization logic in Application instance, that is created before all activities (and other components, such as services, broadcast recievers, content providers).

Just create class like that:

public class App extends Application {

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

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!prefs.getBoolean("firstTime", false)) {
      // <---- run your one time code here
      databaseSetup();

      // mark first time has ran.
      SharedPreferences.Editor editor = prefs.edit();
      editor.putBoolean("firstTime", true);
      editor.commit();
    }
}

All you need for this to work, is put in application tag in AndroidManifest.xml attribute android:name=".App".

<!-- other xml stuff -->

<application ... android:name=".App">

   <!-- yet another stuff like nextline -->
   <activity ... />
</application>

Solution 2

You could try :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
    SharedPreferences.Editor editor = wmbPreference.edit();

    if (isFirstRun){
       // Code to run once
       editor.putBoolean("FIRSTRUN", false);
       editor.apply();
}

Write this in your first activity on create. Then after the code will not execute again.

Solution 3

here's what I do in those situations :

    wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);


    isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);

    if (isFirstRun)
    {

        // Do your magic here

        SharedPreferences.Editor editor = wmbPreference.edit();
        editor.putBoolean("FIRSTRUN", false);
        editor.commit();
    }else{
        //what you do everytime goes here 
    }

hope this helps

Solution 4

Wherever you need to run this code in your app:

  1. Check if boolean firstTime is True in shared preferences
  2. If not

    • Run the one time code
    • Save firstTime as true in shared preferences

Something like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
    // run your one time code here
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}
Share:
39,531
Chintan Soni
Author by

Chintan Soni

Hey visitor, Greetings !!! I am Chintan Soni, a Passionate, Tech savvy, Independent, Self-motivating Mobile Application Developer. Currently serving as TechLead - Android @ ARInspect. Always eager to learn new technologies. Love to help people learn with, whatever I got. Meet my brother: Yash Soni A tech-enthusiast, Gadget Freak and Apple Fanboy. Motto that LIT my life: Learn, Implement and Teach others. Github: https://github.com/iChintanSoni Medium: https://medium.com/@chintansoni My Mentor: Paresh Mayani, Pratik Patel Currently onto: Flutter Kotlin Platforms and Frameworks I have worked on: Android Native Application Development Ionic 3.x Application Development Microsoft ASP.net Web Api 2 using Entity Framework 6.1 (MSSQL Server Database 2014) Node.js (MySQL Server) Google Firebase Augmented Reality Facebook Messenger Bots And what not...!!! Follow me on: Facebook, Twitter, LinkedIn

Updated on August 16, 2020

Comments

  • Chintan Soni
    Chintan Soni almost 4 years

    I want to run a piece of code only once in my application and is when i run it for the first time (newly installed app). How could i do this, can anyone explain giving a piece of code.

    Actually, in my android project i want to create database and insert some values on the first run only. After that, that particular piece of code should not run again. How can i achieve this mechanism through SharedPreferences or Preferences.

    Sample code will be more helpful.