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 ??
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With