How to access "onDestroy" when the app is forced to close (force-Killed)

Asked

Viewed 60 times

1

I have an app and I need it to access a code when the app is forced to close (force-Killed). Below I will leave the code for better understanding, however it is on onDestroy and does not run when the application is forced to close, just what I wanted!

However, I need the user to be able to get out and back in the app and only run the code when it actually closes it!

@Override
protected void onDestroy() {
    //Ao fechar completamente a tela de validação com o campo textValidacao nulo ou incorreto, o cadastro no autenticacao e no banco de dados sao apagados, para evitar o cadastro de usuarios não validados.
    String codigoDigitado = codigoValidacao.getText().toString(); //Pega o texto da caixa de texto8

    if (! codigoDigitado.equals(tokenGerado)) {//Verifica se esse texto e igual, caso ele seja diferente do tokenGerado

        user = UsuarioFirebase.getUsuarioAtual();//Pega usuario atual

        user.delete().addOnCompleteListener(new OnCompleteListener<Void>() { //deleta o usuario no Auth
            @Override
            public void onComplete(@NonNull Task<Void> task) {

                if (task.isSuccessful()) {//Testa para ver se funcionou

                    Log.i("Usuario deletado(auth)","Sim");


                }else{

                    Log.i("Usuario deletado(auth)","Não");

                }

            }
        });

        DatabaseReference firebaseRef = ConfiguracaoFirebase.getFirebase();//Excluir registro no realtime database

        final String identificadorUsuarioLogado = Base64Custom.codificarBase64(UsuarioFirebase.getIdentificadorUsuario());//Id usuario

        DatabaseReference usuarioExcluirRef = firebaseRef//Referencia do usuario
                .child("usuarios")
                .child(identificadorUsuarioLogado);

        usuarioExcluirRef.removeValue();//Remove os valores

        DatabaseReference testeOkRef = firebaseRef.child("usuarios");//Acessa o no usuarios

        testeOkRef.addListenerForSingleValueEvent(new ValueEventListener() {//testa se o usuario foi deletado, vendo se ainda existe o identificadorUsuarioLogado no nó usuarios
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.hasChild(identificadorUsuarioLogado)){//Verifica sem tem o filho no nó

                    Log.i("Usuario deletado(Banco)","Não");
                    Toast.makeText(ValidadorActivity.this, "Usuario deletado(Banco) - Não", Toast.LENGTH_LONG).show();

                }else {

                    Log.i("Usuario deletado(Banco)","Sim");
                    Toast.makeText(ValidadorActivity.this, "Usuario deletado(Banco) - Sim", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    autenticacao.signOut();

    Intent i = new Intent(ValidadorActivity.this, CadastroUsuarioActivity.class);
    startActivity(i);
    finish();
    super.onDestroy();
}
  • 1

    Force-Killed is the same thing as kill -9, is an order for the operating system to kill the running process. As far as I know it is instant and gives the process no opportunity to run onDestroy() or any code that is.

  • Yes, so you needed a q method to run when the Foce force-Killed app

  • 1

    Read the documentation of onDestroy(), I believe he gives no guarantee of being called. And no method will give this guarantee.

  • It is not called even, I look for a similar method, but q execute when force-Killed. I’ve been looking for a while

  • I’m telling you, you’re not gonna find another method that guarantees that.

  • I’ll try another way here, thank you!

Show 1 more comment
No answers

Browser other questions tagged

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