0

I'd like to know what event Android issues when clicking on the top-right X of an Activity. I've tried onStop() and onDestroy(), but they don't get called at all.

Please note that I'm using AppTheme.NoActionBar as app theme, and my Activity extends AppCompatActivity.

Also, is it possible to remove the top-right X button (but keep drop-down menu)?

enter image description here

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
D. Wang
  • 165
  • 1
  • 9

1 Answers1

0

Looking at that image ,you are at "recently used" (or whatever it's called) section of your device. If you are at that screen , onStop() is already called for your activity and when you click the X (close app) button , onDestroy() will be called.

Override those lifecycle methods , onPause onStop etc and place Log statements inside them. Like this : Log.d("lifecycle","onStopCalled"); and check your logcat whats happening

  • Thanks for your reply. I actually overrode onPause(), onStop(), and onDestroy() and traced the code by setting a break point on each method. Based on my observation, when I click on the recent app button (square button at bottom), onPause() get call first and then onStop() gets called, but none of them gets called when I click on the X button. It's very strange. Am I missing anything here? – D. Wang Nov 04 '16 at 21:28
  • that's right. when you click recent apps button(or any button that makes your app dissappear on screen) , first onPause is called and then onStop(). At this point, your application is still alive but your application is not visible, it is at stopped state. After this you have two options : 1. make your app visible on screen again or 2. close your app completely. If you choose option 2 , onDestroy(); will be called. If you choose 1 , these methods will be called : onRestart(); , onResume(); –  Nov 04 '16 at 21:53
  • search "activity lifecycle" on google images and check those schemes. That will help you understand lot better =D –  Nov 04 '16 at 21:54
  • I hope you understood me (not very good english ) . when you clicked X button , onPause() and onStop() didnt invoke because they were already invoked when you navigated to recent apps screen. It is now waiting for either onDestroy or onRestart –  Nov 04 '16 at 22:00
  • Clicking on the recent app button (square button) only reduces the window size of activity, so that you can select one running app from your running apps. It does not make any app disappear. At that stage (as shown in the image I posted earlier), my app is still visible and clicking on the X button will completely close my app, but the problem is that the onDestroy() method does not get called. – D. Wang Nov 04 '16 at 22:22
  • the visible term here is not used very literally. What is meant here is : Your activity , your application is now at background , not the main task anymore. While you are browsing your app , interacting with it , it's the top task of your device. But if you recieve a phone call while browsing your app , that communication application goes to top , moves your application to backstack , calling onPause and onStop on current activity. –  Nov 04 '16 at 22:35
  • about onDestroy() , I guess you just can't notice it's invoked because application is killed and logcat goes offline I think –  Nov 04 '16 at 22:37
  • Not really. I put a Log.d() statement inside my onDestroy() method and also set a break point on it, and then run my app in debug mode and nothing happened. According to Android documents, there is no guarantee that the onDestroy method will be called at all. That's interesting. If Android does not issue any event when the top-right X is clicked, how do I know my app gets killed programmatically. – D. Wang Nov 05 '16 at 00:29