theblitz
theblitz

Reputation: 6881

Attach Eclipse debugger to restarted application

I am testing the onsave/onrestore mthods of my android application.

To do this I phone my device and see that it kills the process and then I hang up. :) I see that it restarts the application.

Question is: how do I cause it to restart in debug mode so I can step through the restore process? Is there a way to tell it to automatically attach to the debugger when starting up?

Upvotes: 9

Views: 1101

Answers (4)

Mike P.
Mike P.

Reputation: 1930

Programmatically: Use waitForDebugger(). Documentation here.

Note that the method returns as soon as the debugger attaches, so it's best practice to place a breakpoint right after that call. Additionally, you can test the debugger attachment status using isDebuggerConnected().

In Eclipse: Open the DDMS perspective of eclipse, select the freshly-restarted app on your device, and then select the debug option. This will attach the debugger to the restarted instance.

On the Device: There is a configuration option under some* handsets that allows you to select an app to be debugged when USB debugging is configured. It's under the Developer Options in your device settings. This will attach the debugger automatically.

*For example, my Galaxy S4 has it, my HTC Rezound does not. I believe it might be a Jelly Bean specific option.

Upvotes: 0

MLProgrammer-CiM
MLProgrammer-CiM

Reputation: 17257

There is a configuration option in Android settings > developer options > debugging > select app to be debugged. What it does is calling the eclipse debugger every time certain application is opened, if it's connected to the adb and the app's project is open.

Upvotes: 1

Randy
Randy

Reputation: 4391

Use android.os.Debug.waitForDebugger();

Basically, start via debugging. Exit out of your app. Set some break points. Enter back into your app (make sure that this line is hit, so put it in your onCreate or somewhere else) and it will re-attach to the running debugger.

Upvotes: 9

ct_rob
ct_rob

Reputation: 521

I don't think there is a way to ensure the app restarts in debug mode. But if you are debugging your own app and don't mind adding debug code for testing you might want to add a Thread.sleep(5000) or something like this at an appropriate place in your startup methods. This should give you enough time to reconnect the debugger via the DDMS. Remove when you are done, of course ;)

Upvotes: 1

Related Questions