Reputation: 139
I have an alert dialog in my application that launches as soon as the application does. I want my alertDialog
only to display when the activity is first launched after it has been installed after that I do not want the alertDialog
to ever pop up again unless the user deletes the application an d then installs it again. I have tried looking to see how can I achieve this method and tried learning to write the proper code but I just can not seem to get it done properly. So can somebody help me achieve this method and get around this issue.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
if (!initialDialogDisplayed) {
Editor editor = preferences.edit();
editor.putBoolean("InitialDialog", true);
editor.commit();
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Alert");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setMessage("Dialog");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
final EditText et = (EditText) findViewById(R.id.editText1);
Button getAnswer = (Button) findViewById(R.id.button1);
getAnswer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (et.getText().toString().length()==0) {
Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();
}else{
EditText et = (EditText) findViewById(R.id.editText1);
String searchTerm = et.getText().toString().trim();
Intent in = new Intent(MainActivity.this, ListView.class);
in.putExtra("TAG_SEARCH", searchTerm);
startActivity(in);
}
}
});
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}}
Upvotes: 3
Views: 759
Reputation: 788
You can store a flag value through SharedPreferences.
SharedPreferences settings = getSharedPreferences("pref_name", 0);
boolean installed = settings.getBoolean("installed", false);
if(!installed){
//showDialog
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("installed", true);
editor.commit();
}
Link : http://developer.android.com/guide/topics/data/data-storage.html#pref
Upvotes: 3
Reputation: 43023
You can use shared preferences to store the fact that the dialog has already been displayed. You should save a value on the display of the dialog and read it to check if you need to display again.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
if (!initialDialogDisplayed) {
Editor editor = preferences.edit();
editor.putBoolean("InitialDialog", true);
editor.commit();
// Display the dialog here
}
Upvotes: 3