Reputation: 54979
I am using shared preferences to store the number of times my application has been launched. Only on the first launch i display an Welcome message telling the user about the new features and changes in that version.
But as i absorbed on reinstalling the app or upgrading the application i am not able to remove the previous shared preferences. i would like to get the dialog when i reinstall the software or upgrade it too.
AppLauncher
public class AppLauncher {
static long launch_count = 0;
private static boolean isLaunch = false;
public static void app_launched(Context mContext) {
System.out.println("I m in AppLauncher");
SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
if (prefs.getBoolean("dontshowagain", false)) {
return;
}
SharedPreferences.Editor editor = prefs.edit();
// Increment launch counter
launch_count = prefs.getLong("launch_count", 0);
editor.putLong("launch_count", launch_count);
System.out.println("launch_count=" + launch_count);
if (launch_count == 0 || launch_count == 1) {
// showLaunchDialog(mContext);
isLaunch = true;
}
if (isLaunch == true) {
showLaunchDialog(mContext);
isLaunch = false;
}
editor.commit();
}
public static void showLaunchDialog(Context mcontext) {
final Dialog dialog = new Dialog(mcontext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.whatsnew);
Button dismisButton = (Button) dialog.findViewById(R.id.dismisButtom);
System.out.println("inside dialog_started");
dismisButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
}
Upvotes: 6
Views: 10951
Reputation: 642
try to listen this Intents:
ACTION_PACKAGE_ADDED ACTION_PACKAGE_CHANGED ACTION_PACKAGE_DATA_CLEARED ACTION_PACKAGE_FIRST_LAUNCH ACTION_PACKAGE_INSTALL ACTION_PACKAGE_REMOVED ACTION_PACKAGE_REPLACED ACTION_PACKAGE_RESTARTED
more on: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
Upvotes: 1
Reputation: 189594
There is no hook that you can use to erase the shared preferences in the case of an update.
Nikolay is right you can save the version number of your app. And compare it with the current version number.
To obtain the current version number call:
this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode
For more info on what information is available in the package info read the Documentation on the PackageInfo and the PackageManager.
Upvotes: 17
Reputation: 15477
If you get don't set dontShowagin you will get false by default .So you want to show dialog and next time not.So just change the value in preference to true so that next time it works.Also you are incrementing the counter without actually incrementing it.Use +1 with the previous one.
SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
if (prefs.getBoolean("dontshowagain", false)) {
return;
}
SharedPreferences.Editor editor = prefs.edit();
// Increment launch counter
editor.putBoolean("dontShowagain",true);
launch_count = prefs.getLong("launch_count", 0)+1;
editor.putLong("launch_count", launch_count);
Upvotes: 1
Reputation: 52956
Instead of saving a boolean
save the version number of the app. If the current app's version number is higher (updated), show your dialog and update the number.
Upvotes: 13