Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing a message dialog only once , when application is launched for the first time in android

Tags:

android

I've developed an application in android and one of the important requirements is to show a message dialog for language support ONLY when the application is running for the first time ,then it will disappear each time the user is running the application again , I've tried to use shared preferences but it didn't work , is there is any other way to do that ??

like image 364
mmoghrabi Avatar asked Mar 21 '12 14:03

mmoghrabi


People also ask

How dialogs are effectively handled in an android application?

Showing a Dialog You should normally create dialogs from within your Activity's onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the “owner” of each dialog.

What's the difference between dialog and AlertDialog in android?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


1 Answers

Use this function in 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: it requires the permission to access a file on a storage: android.permission.WRITE_EXTERNAL_STORAGE

like image 118
asktomsk Avatar answered Oct 02 '22 14:10

asktomsk