How to make an AlertDialog run once at the start of an Activity?

1

I have an alert message that is in the main activity and that is executed at the moment of entering that activity, how can I make the alert message show once, that is, when it is for example in the activity 2 and return to the main activity the message will not be shown again

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Importante");
    builder.setMessage("Recuerda activar el GPS para obtener tu ubicación");
    builder.setPositiveButton("OK",null);
    builder.create();
    builder.show();
}
    
asked by Nelson Emmanuel 09.05.2017 в 02:22
source

3 answers

1

You can control it using SharedPreferences the process is.

To the splashScreen screen set the flag variable firstRun=true

SharedPreferences sp = getSharedPreferences("MY_APP_FLAGS", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("first_run",true).apply();

To the screen where you show the dialog, before showing it you check if firstRun==true and when you show it you set firstRun=false

SharedPreferences sp = getSharedPreferences("MY_APP_FLAGS", 0);
boolean firstRun = sp.getBoolean("first_run",true);

if (firstRun) {
   //Lanzar la Alerta
   firstRun = false;
   SharedPreferences.Editor editor = sp.edit();
   editor.putBoolean("first_run",firstRun).apply();
}
    
answered by 09.05.2017 в 09:12
0

You could use a boolean variable for example and implement it, as follows, it's just a humble opinion

public class MainActivity extends AppCompatActivity {

    private boolean activar = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (activar) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Importante");
            builder.setMessage("Recuerda activar el GPS para obtener tu ubicación");
            builder.setPositiveButton("OK", null);
            builder.create();
            builder.show();
            activar = false;
        }

    }
}
    
answered by 09.05.2017 в 05:08
0
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
    SharedPreferences sp = getSharedPreferences("PRIMERAVEZSP", 0);
    boolean primeravez = sp.getBoolean("primeravez",false);
    if(primeravez)
        {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Importante");
    builder.setMessage("Recuerda activar el GPS para obtener tu ubicación"); builder.setPositiveButton("OK",null); 
    builder.create(); 
    builder.show(); 
   }  
   SharedPreferences.Editor editor = sp.edit();
   editor.putBoolean("primeravez",false);
   editor.apply();
}

In the Splash activity

SharedPreferences sp = getSharedPreferences("PRIMERAVEZSP", 0);
SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("primeravez",true);
       editor.apply();
    
answered by 09.05.2017 в 07:57