Can I call a method when the application is closed?

4

What happens when the user closes the application without calling a pre-defined method in the app, what I mean is when it removes the app from the list of active applications. What happens at the moment?

    
asked by anonymous 20.03.2017 / 04:08

1 answer

7

One option is to explore the life cycle of your core activity. For example, when the "Delete All" feature is used, in this case the main activity of your application will call the onDestroy() " that is part of the life cycle of an activity . Therefore, you can enter the action to "remove a child" within onDestroy() . Enter the method in your MainActivity and run a test using Toast . Here's 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 .

    
20.03.2017 / 05:05