Search code examples
androidsqliteopenhelper

onDowngrade() method has not been called


I need to override the onDowngrade method, so that calls onDowngrade(), whenever the older version of database in my app replaces the existing version of the app

i.e. (newer version db) < (older version db).

Example: When I tried to install the new app with the database version 3 will replace the current or already installed app with the database version 2, never called this onDowngrade method.

I hope my question is very clear. Please take a chance to give some idea about this method by answering this question.

MY new version of app Source code:

public class MyDatabase extends SQLiteOpenHelper
    {
        private static final int DB_VERSION     =   10;

        public MyDatabase(Context context) 
        {
            super(context, DB_NAME, null, DB_VERSION);
        }
       @Override
       public void onCreate(SQLiteDatabase db) 
        {
            Log.d("Method","onCreate called");

                .....
                .....
                .....

        }
     @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.d("Method","onUpgrade called");
                 .....
                 .....
                 .....
        }
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
        Log.d("Method","onDowngrade called");
                 .....
                 .....
                 .....
        }

}

Manifest.xml

VersionCode: 10

MY old version of app Source code:

 public class MyDatabase extends SQLiteOpenHelper
    {
        private static final int DB_VERSION     =   9;

        public MyDatabase(Context context) 
        {
            super(context, DB_NAME, null, DB_VERSION);
        }
       @Override
       public void onCreate(SQLiteDatabase db) 
        {
            Log.d("Method","onCreate called");

                .....
                .....
                .....

        }
     @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.d("Method","onUpgrade called");
                 .....
                 .....
                 .....
        }
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
        Log.d("Method","onDowngrade called");
                 .....
                 .....
                 .....
        }

}

Manifest.xml

VersionCode: 9

Finally once again my query is I replaced the new app with the old app (which have corresponding source code as above).

But the Older version of app doesn't call the onDowngrade().


Solution

  • Found answer for this specific question.

    in my AndroidManifest.xml file minSdkVersion is 8 as below and which never calls onDowngrade().

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    

    , but onDowngrade method will be called only when minSdkVersion must be greater than or equal to 11 as below.

    Solution:

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />