Showing the message dialog only once when the application is launched for the first time in android

I developed the application in android, and one of the important requirements is to display a message dialog for language support ONLY when the application is running for the first time, and it will disappear every time the user starts the application again, I tried to use the general settings, but this is not worked, is there any other way to do this?

+6
source share
3 answers

Use this function in the onCreate handler:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... if (isFirstTime()) { // show dialog } ... } /*** * Checks that application runs first time and write flag at SharedPreferences * @return true if 1st time */ private boolean isFirstTime() { SharedPreferences preferences = getPreferences(MODE_PRIVATE); boolean ranBefore = preferences.getBoolean("RanBefore", false); if (!ranBefore) { // first time SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("RanBefore", true); editor.commit(); } return !ranBefore; } 

Note : this requires permission to access the file on the repository: android.permission.WRITE_EXTERNAL_STORAGE

+15
source

General preferences will be the recommended way, however you can also write the parameter to the local sqlite database or write it to a file that will be stored on the internal device storage (SD card).

0
source

You return "! RanBefore", of course, you should only return "ranBefore". Also, you can always get the same general settings using:

 SharedPreferences settings = getSharedPreferences("yourSharedPreferanceName", 0); 
-1
source

Source: https://habr.com/ru/post/911305/


All Articles