Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show an alert dialog only on the first run of my application?

I have an alert that I want to display only the first time right after starting the application for the first time.

How can I do that?

like image 786
waa1990 Avatar asked Mar 23 '11 18:03

waa1990


People also ask

What is the difference between an alert and an alert 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 .

How do I show alerts in dialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

How do I show custom alerts?

Add custom_layout. xml in that activity in which you want to show custom alert dialog here it is added in MainActivity.


1 Answers

There are a few ways to do this, but perhaps the easiest is just to check a flag in a SharedPreferences object and set it once the alert is shown.

SharedPreferences

Something like

public class MyActivity extends Activity {

public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state){

   super.onCreate(state);
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean dialogShown = settings.getBoolean("dialogShown", false);

   if (!dialogShown) {
     // AlertDialog code here

     SharedPreferences.Editor editor = settings.edit();
     editor.putBoolean("dialogShown", true);
     editor.commit();    
   }
}
like image 70
Brian Cooley Avatar answered Oct 24 '22 19:10

Brian Cooley