I just want to ask, when does the debugger get disconnected?
I am debugging my app on the device and when I go to the background and wait some time (around 2 to 5 minutes), my app is being disconnected and I want to handle this case because it makes my app crash when I go back to the app.
Thanks!
Hi guys I know its a little late but for everyone else searching here what you have to do:
Go on your device -> settings -> developer options -> select debug app -> turn on 'Wait for debugger'
Now on Android Studio run the app in debug mode, close it and wait for the process to close (causing it to detach from the debugger) and reopen it from the recent drawer. You will get a waiting for debugger message on the device.
Go on Android Studio and press the attach debugger to process button , select the process and done :D
The same thing happened to me. In my case, the debugger was getting disconnected every time the system destroyed the app after being stopped for a while (i.e. in the background). This happens when the system needs more memory/resources for the foreground app or when your app has been stopped for too long, cf. http://developer.android.com/training/basics/activity-lifecycle/recreating.html.
The best way I have found to debug this is to add Log.d() logging (http://developer.android.com/reference/android/util/Log.html#d(java.lang.String, java.lang.String). After I started logging all lifecycle events, I found that in my case the following was happening:
I switch to a different foreground app, causing my app to go in the background in a stopped state
onPause()
onStop()
debugger disconnects (typically after a few minutes, sooner if using a memory intensive foreground app). Note that onDestroy() doesn't get called here but activity does get destroyed.
I re-launch my app, debugger is no longer attached
onCreate() - system tries to re-create activity using savedInstanceState
onStart()
onResume()
Because the system was the one that destroyed the activity, when it re-creates the activity it tries to restore the state of all Views. If you need to save additional information, be sure to override the onSaveInstanceState() method which gets called before the activity is destroyed, and then recover the saved information by accessing the savedInstanceState Bundle in the Activity's onCreate() method.
Hope that helps.
Related
We have one very difficult to reproduce problem with an app. The use case is as follows:
User opens app
User leaves app in background
User openes 5 to 7 more apps
System kills our app
When user tries to resume app, app crashes due to NullPointerException
I was trying to use console log with Application class method onTrimMemory() and onLowMemory() but this methods are not being called. Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?
You can save whatever states you need in onSaveInstanceState() and restore them in onRestoreInstanceState(). onDestroy() is what the system will most often call to clear room while your Application is in the background, but it is not guaranteed to be called, so it is better to have already saved your states in onSaveInstanceState().
Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?
Not really. Depending on circumstances, onDestroy() of running activities and services will be called.
We have one very difficult to reproduce problem with an app
That should be reproducible in a matter of seconds:
Run your app.
Switch to another app.
Kill your app's process from your IDE (e.g., "Terminate Application" toolbar button in "Android Monitor" tool window).
Try returning to your app from the overview screen (a.k.a., recent-tasks list)
When debugging an app in Android Studio, if I have an onDestroy method in the main activity and then hit the Back button, onDestroy will get called. However, Android Studio's still shows debugging. What isn't clear is whether this is only showing a debug connection between Android Studio and the device. If I go to App > Settings and force the app to close, the debugging session is terminated. This makes it seem like my app was still running in spite of onDestroy being called. But I have read elsewhere that Android will keep an app in memory for performance reasons. If the user wants to restart the app right away and the app hasn't been garbage collected, apparently it is more efficient to just restart the app that was previously destroyed.
But this raises the question as to whether you can really tell if your app has really been terminated when you press the Back button on the main activity. If onDestroy is called, does this really mean it has been terminated? I understand that you could hold a reference to some object in your code that doesn't get released but onDestroy can still get called but the app remains unterminated due to the referenced object that prevents it from being garbage collected.
Even if you write a bare bones app that does nothing but show a blank activity and hit the Back button, the heap still shows references. So I'm at a loss as to how you can tell with certainty that your app has been terminated.
This makes it seem like my app was still running in spite of onDestroy being called.
The sooner you stop thinking in terms of "app", the more success you will have as an Android developer.
In this case, you seem to be conflating "app" with "process". Your process will continue running for some time, even when you have no UI in the foreground.
But this raises the question as to whether you can really tell if your app has really been terminated when you press the Back button on the main activity.
An "app" is not terminated. A process is terminated. Your process will not be terminated immediately when the user leaves your UI by any means (BACK, HOME, whatever).
If onDestroy is called, does this really mean it has been terminated?
It means that whatever component onDestroy() was called on was destroyed, such as an Activity or Service.
So I'm at a loss as to how you can tell with certainty that your app has been terminated.
Android does not inform you when your process will be terminated, as there are many possible reasons for the process being terminated, including the user telling Android to get rid of your process.
I have a bug I'm trying to analyze that occurs when the Activity's onDestroy() method is called after hitting the back button. I've put breakpoints in the offending code (using Eclipse). The debugger pauses the app at the breakpoint, but the Android system also takes the app off the screen and returns to the phone's homescreen. After the app is paused for about 10 seconds, the app's thread seems to get destroyed by the Android system because the debugger suddenly disconnects.
Any ideas on how to keep the Android system from doing this? I need to keep the app alive so I can step in the debugger, look at variables, etc.
Phone is running Android 2.3.5.
A workaround that I found is to put a startActivity() call into onDestroy() (before super.onDestroy()) that starts a dummy instance of the Activity, just to keep the app alive. The Android system won't garbage collect the app thread because there is still an Activity running within it (the new dummy Activity). This in turn allows you to debug things because the debugger's connection to thread won't be lost.
If the phone pops up a dialog saying the app is not responding (Force Close or Wait), don't click Wait, just leave it alone. It seemed that clicking Wait caused the app thread to be killed and a new thread was created for the dummy Activity.
You can try a breakpoint on super.onDestroy(), but I suspect you'll have the same luck. :(
Android won't let you linger in onDestroy, it will timeout, so try to accomplish your shutdown more quickly. onDestroy() is intended only for freeing resources and isn't always called before termination; data should be persisted in onPause() or onStop().
https://developer.android.com/training/basics/activity-lifecycle/stopping.html
Addendum: Other options include using a background service for some of the work or to manually handle the back button to give yourself more time, but it could negatively impact user experience.
http://www.stanford.edu/class/cs193a/03/
sent from my phone, please cut my thumbs some slack.
I've chosen to save the persistent application state in the OnPause() method to my database because OnPause() is guaranteed to be called before the application gets killed according to the documentation of the activity life cycle.
Now I am facing following behaviour using eclipse and avd emulator (api level 8):
1. I start my application via eclipse - Instance1
2. I start my application via eclipse again - Instance2
Now Instance1 is being terminated without calling OnPause()!
Could somebody please explain me why OnPause() is not being called? I thought it is guaranteed to be called always. If this is not the case, maybe because of the way eclipse terminates Instance1 process, then I would like to know if I could change this. Thanks a lot.
When you start the application via eclipse, it will effectively rip the rug out from any instance of the same app already running on the device or an emulator. This will never happen in practice, only when running from eclipse.
So you can plan on onPause() always being called.
what you mean you start application via eclipse - instance 1 and 2?
when click on run button, emulator (in your case or a real device) runs your application. please don't run it in other AVD (if i get correctly based on your instance 1 and 2). If you have put log code in your onCreate() and onPause() methods, For example Log.i(TAG, "I'm in onCreate()!");, you should see it in logcat. therefore, when you launch your app you will see onCreate message and when you click on home button for example, you will see onPause() message.
This is the way which is guaranteed to be called.
We know that when the system runs out of resources, an activity in background serializes its state and gets killed by the OS. When we resume it, the OS recovers the activity state by savedInstanceState passed to onCreate method. Considering we are responsible for handling what is going to be serialized/recovered, I'd like to have my activity killed in order to test the code I created for recovering. How can I achieve that? Forcing the application to be killed through the applications menu doesn't help.
Rotate your device (or emulator). Android saves, destroys, and re-creates the activity in the new orientation.
Download a task manager that kills the process in a less destructive way than "Force stop" in "Manage applications" settings. Example: GO task manager.
The task manager will kill the app (and the debug) but somehow not the activity stack (don't know why).
When you'll relaunch the app again, onCreate will be invoked with the last saved bundle/state.
The disadvantage of this solution, compared to Darrell's, is that you cannot debug it.
The advantage of this solution, compared to Darrell's, is that it is more close to real life scenario.
You can kill it from Eclipse also.
Go to the Android view. YOu should see the list of processes in the Devices tab.
Click on your process and then click the little "STOP" button.
Instant death!
FYI you can also attach the debugger this way by clicking on the little green bug