0
Instead of the app showing the interface below, it pulls the Onrestart method directly and displays "Program restarted" when I open the app. How do I fix it ?
Code :
TextView tInforme;
EditText tValor;
Button btDescobrir;
protected static final String CATEGORIA = "Exercicio";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(CATEGORIA, getClassName() + ".onCreate()) chamado: " + savedInstanceState);
TextView t = new TextView(this);
t.setText("Programa reiniciado");
setContentView(t);
setContentView(R.layout.activity_exercicio1);
tInforme = (TextView) findViewById(R.id.tInforme);
tValor = (EditText) findViewById(R.id.tValor);
btDescobrir = (Button) findViewById(R.id.btDescobrir);}
public void setbtDescobrir(View v) {
String guessStr = tValor.getText().toString();
int theGuess = Integer.parseInt(guessStr);
int tValor = theGuess%2;
if (tValor == 0){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("O número é : ");
alertDialog.setMessage("O número é par !");
alertDialog.show();
}
else if (tValor == 1){
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("O número é : ");
alertDialog.setMessage("O número é ímpar !");
alertDialog.show();
}
}
@Override
public void OnRestart()
{
super.onRestart();
Log.i(CATEGORIA, getClassName() + ". onRestart() chamado.");
}
private String getClassName()
{
String s = getClass().getName();
return s.substring(s.lastIndexOf("."));}
}
If that
OnRestart()
is not doing anything because he is there?– viana
The goal is to make sure that when it is called, it shows on the screen that the program has been restarted as a warning. Then instead, it is appearing straight on screen without letting the program interface appear, getting in the background
– Henry Lemon
This happens because your Onrestart does nothing (actually it does, which is just generate a log, but you will only see it in the logcat of Android Studio), while the Oncreate method is always called, regardless of whether it is the first run of the app or whether it was already running, so you will always see the message "Program restarted"
– Márcio Oliveira
Hmm, so I think it’s a problem to relocate that part of the "reset program" from there...
– Henry Lemon
@Márciooliveira I said does nothing so relevant... at first I had not seen something that would compromise the functioning. It is important for Paul to take a look at life cycle of an activity.
– viana
I’ll take a look. I’m beginner and I’m taking a dough dick to mess with it, obg
– Henry Lemon
In fact, I made a mess. Oncreate is called only the first time the app runs, then comes the call to Onstart. Onrestart only runs when the app is placed in the background and then returned to the foreground (before being destroyed).
– Márcio Oliveira