Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method is being called when I close an app

I read all about the activity lifecycle and it's methods.

I still couldt find an answer: When I close my app, from the "open apps" menu (in galaxy4 it's a long press on the home button, in nexus5 it's the right button ...) what method is being called? if any?

In other words, my activity is launching a service. I want to terminate the service if the app (activity) is being closed. (onDestory is not reliable at all as said many times here before)

Thanks in advance!

like image 616
nivklein Avatar asked Jul 15 '14 13:07

nivklein


People also ask

Which method is called when app is closed?

A Service method onTaskRemoved will be called when we remove app from recent items.

Which method is called when app goes in background?

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again.

What method is called in an activity when it is destroyed?

OnDestroy. OnDestroy is the final method that is called on an Activity instance before it's destroyed and completely removed from memory. In extreme situations Android may kill the application process that is hosting the Activity, which will result in OnDestroy not being invoked.

What is finish method in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.


1 Answers

onPause() is the only method that is called always. From onPause() state Android OS can kill this app for many reasons bypassing onStop and onDestroy. I don't think we can control this behavior of unexpected termination smoothly. Service can check for the state of the application periodically.

like image 89
ashishpinninti Avatar answered Sep 20 '22 13:09

ashishpinninti