Is it possible to call a method when the application is closed?

Asked

Viewed 250 times

4

What happens when the user closes the app without calling a preset method in the app, what I mean is when it removes the app from the list of active apps.What happens at this point in time is?

inserir a descrição da imagem aqui

  • From what I understand you want the "Erase all" method to be called always before the application ends. Since this is difficult for several questions, why don’t you call this method before the application starts? I mean, if the table is filled in before the application starts inserting data then it should be deleted because it is 'garbage' from the previous use.

1 answer

7


One option is to explore your own life cycle of its main activity. For example, when the "Erase All" device feature is used, in this case the main activity of your application will call the method onDestroy() which is part of the life cycle of a Activity. Thus, you can enter the action to "remove a Child" within the onDestroy(). Insert the method into your MainActivity and take a test using a Toast. Take an example:

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this,"Aplicação destruída com o onDestroy()",
        Toast.LENGTH_SHORT).show();
}

Note: For your purpose, there are other ways to do this, for example using Services.

  • 3

    Thank you Ack Lay!

  • although being what I wanted will not work very well has the probability still of the mobile suddenly turn off.

  • 1

    The answer agrees and answers your question efficiently, but there are other, perhaps more effective ways to do this, for example using Service.

  • So the answer was very useful, more after I thought about this problem, thanks for the new tip!

  • 1

    Your question is kind of an XY question, because the most appropriate method of verifying the amount of active users is using onDisconnect(). You have a specific article about this: https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html

  • .info/Connected solved my problem

Show 1 more comment

Browser other questions tagged

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