After you have built an application , you need to be able to debug it and see what is going on inside your code . One of the handiest ways to be able to see inside your code it through the use of break points. Breakpoints allow you to pause the execution of your code at specific location and see what is going on (or what is going wrong). Let's take a look at how to use breakpoints in Android Studio.
Setting Breakpoints
Breakpoints are mechanism by which you can tell Android Studio to temporarily pause execution of your code , which allows you to examine the condition of your application . This means that you can check on the values of variables in your application while you are debugging it. Also , you can check whether certain lines of code are being executed as expected - or at all.
To tell Android Studio that you want to examine a specific line of code during debugging , you must set breakpoint at the line. Click the margin of the editor tab next to line of code you want to break at , to set a breakpoint. A red circle is placed in the margin , and the corresponding line is highlighted in red , as show in Figure
You can also set breakpoint by placing your cursor in the line of code. where you want it to break and clicking Ru->Toggle Line Breakpoint. Notice that the term used it toggle , which means that any breakpoints you set can be turned off the same way you turn them on. Simply Click an existing breakpoint to remove it from your code.
NOTE Android studio only pauses execution at breakpoints when you debug your application - not when you run it it. This means you must use the green arrow with the bug behind it (or select Run -> Debug 'app' , or press Shift + F9).
Let's say that you do not know the exact line of code where you want the break to be . You might want to check on the condition of your code when a specific method is called . you can set a method breakpoint by selecting Run -> Toggle Method Breakpoint . A method breakpoint is represented by a red circle containing four dots placed at the method signature.
Notice in the lower left-hand area of Figure that Android Studio has issued a warning that method breakpoints can dramatically show down debugging . This is because method breakpoints do more than simple breakpoints in their default state. By default , method breakpoints are set apart from simple breakpoints. Android Studio pauses execution when the method is hit , and it also automatically sets a corresponding breakpoint and pauses at the end of the method.
NOTE Using a method breakpoint is different from setting a simple breakpoint at the signature of the a method . You easily click in the margin of the editor tab at the signature of a method to set a simple breakpoint there However , this one will get hit when execution enters the method. Also , a method breakpoint automatically pauses when the method is exited.
Thus far, I've discussed simple and method breakpoints . However , there are two other types of breakpoints that you examine in the section: temporary breakpoints and conditional breakpoints.
Temporary Breakpoints :-
A temporary breakpoint is useful when you are trying to debug a large loop, or you just want to make sure a line of code being hit execution. To set a temporary breakpoint, place your cursor at the location in the code where you want it break and select Run -> Toggle Temporary Line Breakpoint. Notice that a red circle containing a 1 is now placed in the margin.
The 1 in the red circle represents the fact that Android Studio only stops at this breakpoint the first time youe code enters it . After that , the line is executed as though there is no breakpoint set . This can be very useful if you want to ensure line within a loop is being hit , but you don't want to stop at line every time it is executed.
However , let's say that you want to ensure that line within a loop is only being called when a specific variable is set to true ( or something similarly complex ). In such a case , you can use a conditional breakpoint.
Conditional Breakpoints :-
A condition breakpoint is a breakpoint at which Android Studio only pauses when specific conditions are met . To set a conditional breakpoint , first set a simple breakpoint at the line of the code you want to examine , the right-click the simple breakpoint to bring up the condition context menu.
From here you can set conditions that tell Android Studio when to pause at a breakpoint . For example , you can tell Android Studio to only pause at a line of the code when your variable named foo equals true. You would then set the condition in the breakpoint to
foo == true
Conditional breakpoints are extremely useful in diagnosing intermittent issues in complex code blocks.
Setting Breakpoints
Breakpoints are mechanism by which you can tell Android Studio to temporarily pause execution of your code , which allows you to examine the condition of your application . This means that you can check on the values of variables in your application while you are debugging it. Also , you can check whether certain lines of code are being executed as expected - or at all.
To tell Android Studio that you want to examine a specific line of code during debugging , you must set breakpoint at the line. Click the margin of the editor tab next to line of code you want to break at , to set a breakpoint. A red circle is placed in the margin , and the corresponding line is highlighted in red , as show in Figure
You can also set breakpoint by placing your cursor in the line of code. where you want it to break and clicking Ru->Toggle Line Breakpoint. Notice that the term used it toggle , which means that any breakpoints you set can be turned off the same way you turn them on. Simply Click an existing breakpoint to remove it from your code.
NOTE Android studio only pauses execution at breakpoints when you debug your application - not when you run it it. This means you must use the green arrow with the bug behind it (or select Run -> Debug 'app' , or press Shift + F9).
Let's say that you do not know the exact line of code where you want the break to be . You might want to check on the condition of your code when a specific method is called . you can set a method breakpoint by selecting Run -> Toggle Method Breakpoint . A method breakpoint is represented by a red circle containing four dots placed at the method signature.
Notice in the lower left-hand area of Figure that Android Studio has issued a warning that method breakpoints can dramatically show down debugging . This is because method breakpoints do more than simple breakpoints in their default state. By default , method breakpoints are set apart from simple breakpoints. Android Studio pauses execution when the method is hit , and it also automatically sets a corresponding breakpoint and pauses at the end of the method.
NOTE Using a method breakpoint is different from setting a simple breakpoint at the signature of the a method . You easily click in the margin of the editor tab at the signature of a method to set a simple breakpoint there However , this one will get hit when execution enters the method. Also , a method breakpoint automatically pauses when the method is exited.
Thus far, I've discussed simple and method breakpoints . However , there are two other types of breakpoints that you examine in the section: temporary breakpoints and conditional breakpoints.
Temporary Breakpoints :-
A temporary breakpoint is useful when you are trying to debug a large loop, or you just want to make sure a line of code being hit execution. To set a temporary breakpoint, place your cursor at the location in the code where you want it break and select Run -> Toggle Temporary Line Breakpoint. Notice that a red circle containing a 1 is now placed in the margin.
The 1 in the red circle represents the fact that Android Studio only stops at this breakpoint the first time youe code enters it . After that , the line is executed as though there is no breakpoint set . This can be very useful if you want to ensure line within a loop is being hit , but you don't want to stop at line every time it is executed.
However , let's say that you want to ensure that line within a loop is only being called when a specific variable is set to true ( or something similarly complex ). In such a case , you can use a conditional breakpoint.
Conditional Breakpoints :-
A condition breakpoint is a breakpoint at which Android Studio only pauses when specific conditions are met . To set a conditional breakpoint , first set a simple breakpoint at the line of the code you want to examine , the right-click the simple breakpoint to bring up the condition context menu.
From here you can set conditions that tell Android Studio when to pause at a breakpoint . For example , you can tell Android Studio to only pause at a line of the code when your variable named foo equals true. You would then set the condition in the breakpoint to
foo == true
Conditional breakpoints are extremely useful in diagnosing intermittent issues in complex code blocks.
Comments
Post a Comment