Probably a stupid question, but found nothing on the Internet. (

How do I know that the application is launched for the first time in the following cases:

  1. First install and launch.
  2. Reinstallation and first launch.
  • My data is stored in preferences, and I need to understand whether the application was reinstalled so that we can clean up this data so that there are no conflicts - DarkVss
  • 3
    Keep the version of the application in preferences, if it is different from the current one, it means it was updated ... - Gorets
  • Hmm ... option ... Chet stupid))) - DarkVss

2 answers 2

To check on the first launch try using SharedPreferences. When you first start, write any value in them, and then check it http://startandroid.ru/ru/uroki/vse-uroki-spiskom/73-urok-33-hranenie-dannyh-preferences.html

    For example:

    1) Let's go to Google.

    2) drive in

    first launch android

    3) In the first link we find the following answer:

    This can be done using SharedPreferences . Those. when starting the application, we check the boolean variable. If it is not there, then the first launch means (or the user deleted all application data). After changing the value of the variable and the code will not run at the next launches.

    Code:

    public class MyActivity extends Activity { SharedPreferences prefs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (prefs.getBoolean("firstrun", true)) { // При первом запуске (или если юзер удалял все данные приложения) // мы попадаем сюда. Делаем что-то //и после действия записывам false в переменную firstrun //Итого при следующих запусках этот код не вызывается. prefs.edit().putBoolean("firstrun", false).commit(); } } }