Delete all Data by sending a new Android App Update

Asked

Viewed 59 times

0

I made some changes in my app, and I need to erase all the old data. What happens is, when updating, the old data is still stored.

The only solution I could find, which worked, was to change the package name. But I can’t do this because I’ll have to publish a new app if I change the package name

What also worked is if I go in the settings and erase the App Data manually.

Question: Is there a class for when the user updates the app, delete all data in the app?

  • data where? Shared Preferences? Sqlite? Firebase?

  • From Firebase (Information that is stored on the mobile, not on the server) but by guarantee I need to delete everything that is stored in the App, because even uninstalling, and installing again, the old data continues to.

1 answer

0


You will need to create this control in your app’s Start Activity to check the app version and delete what you want if you change the version

This commented in the code how the control should work.

Activity LAUNCHER:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Pegar acesso ao SharedPreferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // Pegar a ultima versão armazenada, se for uma versão anterior a implementação deste
        // controle, vai retornar "0" e fara a limpeza da mesma forma.
        String lastVersion = prefs.getString("APP_VERSION", "0");

        // Pegar a versionName do build.gradle
        String currentVersion = getAppVersion(this);

        // Verificar se mudou a versão
        if (!lastVersion.equals(currentVersion)) {
            // Apagar SharedPrefs
            // Apagar BD
            // Apagar o que for preciso

            // Colocar no Prefs a versão atual para só limpar de novo na proxima att
            prefs.edit().putString("APP_VERSION", currentVersion).apply();
        }
    }

    /**
     * Rotina para pegar o versionName do build do app
     * @param context
     * @return version name do arquivo build.gradle
     */
    private String getAppVersion(Context context) {
        String result = "";

        try {
            result = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0)
                    .versionName;
            result = result.replaceAll("[a-zA-Z]|-", "");
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(context.getClass().getSimpleName(), e.getMessage());
        }

        return result;
    }
}

Build.Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "br.com.a2sistemas.myapplication"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 2 // Trocar este numero a cada att
        versionName "2.0" // Trocar este numero a cada att
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
  • I was testing here, and this giving error in if (!lastVersion.equals. Appears "Can not resolve "equals", Voce knows what can be?

  • You created the lasVersion variable as String ? the Equal is a method of the String class to buy values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.