(Android Studio) Problems with Onrestart eating the main interface

Asked

Viewed 159 times

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 ? ProblemaOnRestart

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?

  • 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

  • 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"

  • Hmm, so I think it’s a problem to relocate that part of the "reset program" from there...

  • @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.

  • I’ll take a look. I’m beginner and I’m taking a dough dick to mess with it, obg

  • 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).

Show 2 more comments

1 answer

0


I believe the problem is connected to this part of the code.

setContentView(t);
setContentView(R.layout.activity_exercicio1);

'Cause you’re putting as your main view the:

 TextView t = new TextView(this);
t.setText("Programa reiniciado");

Even you calling the

setContentView(R.layout.activity_exercicio1);

It will not work because it draws only the first setContentView. I believe that the correct thing to do is to remove this line:

setContentView(t);
  • I think I fixed here, the screen is not getting in the background anymore, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.