Willi Mentzel
Willi Mentzel

Reputation: 29844

No SQLiteOpenHelper.onCreate call after re-install

For testing purposes I manually remove the app I am developing from my phone.

When I re-install it using Android Studio, the onCreate method from SQLiteOpenHelper is not called. Only if I explicitely purge the app's memory in the system's app settings, it is called on next start-up.

What is happening? Is the app not completely removed during deinstallation?

Edit 1: how can this database file persist, if the app is deinstalled? this can cause some unwanted behaviour! what's the best way to know that the app was just installed and when to reset everything?

Edit 2:

My current code looks like this:

public class SQLiteDbHelper extends SQLiteOpenHelper {

    // Yes, previous db versions would get upgraded, but this 
    // should not matter for the question, because I deinstall 
    // and reinstall.
    private static final int DATABASE_VERSION = 2; 

    private static final String DATABASE_NAME = "AppDB";

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("info", "============================");
        Log.i("info", "SQLiteDbHelper.onCreate(...)");
        Log.i("info", "----------------------------");

        // ...

        Log.i("info", "============================");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("info", "============================");
        Log.i("info", "SQLiteDbHelper.onUpgrade(...)");
        Log.i("info", "oldVersion = " + oldVersion);
        Log.i("info", "newVersion = " + newVersion);
        Log.i("info", "============================");

        // ...
    }

   // ...
}

Upvotes: 0

Views: 358

Answers (2)

Willi Mentzel
Willi Mentzel

Reputation: 29844

The problem was in my AndroidManifest.xml file:

<application
    android:allowBackup="true"

onCreate was never called because the database file still existed. I set allowBackup now to false and everything is removed upon deinstallation.

Upvotes: 1

Weava
Weava

Reputation: 496

You might check out This SO post

tl;dr, installing the database file (.db) to an sd card, or any external storage on the device will persist that database file through all uninstalls.

Check to see if you are installing the database file to an external storage device from the application.

EDIT In your SQLiteOpenHelper class, the constrcutor's second argument defines the file's name and location, for instance:

public class MyDBHelper extends SQLiteOpenHelper {

    public static final int DB_VERSION = 1;

    public static final String DB_NAME = "myDb.db";

    public MyDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
...
}

Would define a database that is associated only as your app's data. This would be deleted if you uninstalled the application.

Upvotes: 1

Related Questions