Make the dialog box appear once… here is a solution to the problem.
Make the dialog box appear once
I have an alert dialog in my app that starts as soon as the application starts. I want my alertDialog to only show up on first launch after the activity is installed I don’t want alertDialog
to pop up again unless the user removes the app and then installs it again. I tried to see how to implement this approach and tried to learn to write the right code, but I couldn’t seem to do it properly. Then someone can help me implement this method and solve this problem.
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();
}}
Solution
You can store flag values 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