Find out if Activity has been closed

Asked

Viewed 236 times

1

Good night! I’m a beginner on android and I’m developing a chat app where two users connect through a queue based on their chosen preferences. When one of the users disconnects from the chat (end Activity) I want to save the status in firebase to inform the other user that the person with whom he was talking came out conversation.

How can I check whether my Activity has been destroyed or not? I tested using a boleana variable with active name and set it as trrue in onStart and false in onDestroy. I have also tried using the isDestroyed method to see if Activity has been terminated. But I am failing at this.

I don’t know where I do this check to test the value of these boolean variables. I tried to check onStart but it didn’t work. What should I do? Below are the methods I’ve done.

public boolean appAberto(){
        //Se foi destruido retorna false, se esta aberto retorna true
        if (ChatPrivadoActivity.this.isDestroyed()){
            //retorna true se foi destruido
            //isFinishing Verifica se a activity esta em procersso de finalização
            Toast.makeText(this,
                    "ENCERROU", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

I called this function on onStart and did not return anything even after I closed Activity. Then I tried this:

@Override
    protected void onStart() {
        super.onStart();
        recuperarMensagensConversa();
        ativo = true;
    }
    
    @Override
    protected void onStop() {
        super.onStop();
       ativo = false;
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ativo = false;
    }

But I didn’t succeed because I didn’t know exactly where to test this variable I created. What I’m doing wrong?

  • Puts this variable Boolean only in onResume and onPause, onResume true and onPause false, I believe it works

2 answers

1

I found a better way to detect if the app was closed by the user. I used the isfinishing and isDestroyed methods within the onStop method.

   @Override
protected void onStop() {
    super.onStop();

    if (ChatPrivadoActivity.this.isFinishing() || ChatPrivadoActivity.this.isDestroyed()){           

            Toast.makeText(this,
                    "onStop-fechou o app", Toast.LENGTH_SHORT).show();                
            finish();            
    }
}

0


I believe the simplest way to function, based on Life Cycle of Activity. is the following:

Add the onResume method

protected void onResume() {
    super.onResume();
    //Aqui você pode recuperar os dados do chat caso tenha
    //e enviar pro seu database que a Activity está ativa
}

The onResume method is always called when starting an Activity or when the status of the Activity that is paused, stopped or destroyed is summarized.

Add the onPause method

   protected void onPause() {
        super.onPause();
        //Aqui você pode salvar informações do chat no seu database
        //e enviar pro database que a Activity não está em uso

    }

onPause is always called when the user closes the application, gets in the background, another Activity is started, the Activity is destroyed...

  • 1

    Thank you so much! It worked for me. I set "active = false" in onPause as you indicated and inside the onDestroy method I did a check to update the value inside firebase and so I was able to notify the user that the other user (with whom he was talking) closed the chat window.

Browser other questions tagged

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