Conditional to change activity

0

Hello, I have a problem, I want to create a conditional on an activity, inside a button, to open another activity in my app.

  

But how would that conditional be?

My app has buttons in the main activity that leads to other activityies. One of these activityies is a register, where the user fills some data in some Editexts and saves them, I used the SharedPreferences to Save this data.

//Exemplo
SharedPreferences anonacpref = getSharedPreferences("anonac", Context.MODE_PRIVATE);
SharedPreferences.Editor edanonac = anonacpref.edit();
edanonac.putString("anonac_texto", anonactxt.getText().toString());
edanonac.commit();

Anyway, I would like that when the data was already saved, when the user pressed the button in the main activity he would be taken to another activity instead of being taken to the activity of filling the database. >

  

How do I test if I already have something saved in SharedPreferences?

    
asked by anonymous 01.10.2015 / 22:24

1 answer

1
SharedPreferences anonacpref = getSharedPreferences("anonac", Context.MODE_PRIVATE);
String texto = anonacpref.getString("anonac_texto", null);
if (texto != null) {
    System.out.println("Já preenchido com: " + texto);
} else {
    System.out.println("Texto não encontrado em shared preferences.");
}

The second parameter of getString() is the default value, to be returned if it does not find the value of "anonac_texto" in the shared preferences file. In case I'm using null .

    
02.10.2015 / 00:59