I know I could set a breakpoint at every line where my code changes the variable, but is there an option such as right-clicking a variable (to "Add to Watches") that would stop when a variable changes value?
I think C++ has this option. See this.
And Eclipse? See this.
Is this implemented in AS?
You can break on value changes of some variables in Android Studio 1.1.0.
Android Studio calls them 'Java Field Watchpoints'.
From the breakpoints windows, (Run -> "View breakpoints...") or Ctrl+Shift+F8,
you can add "Java Field Watchpoints" from the plus in the top left corner, and then select your class and variable.
If the goal is simply to set a watchpoint where execution halts if a variable's value changes, there are two methods:
Pressing Ctrl-shift-F8 to set a watchpoint brings up this dialog:
That looks more powerful but it's confusing because it lacks the option I need; and there's an extra step: filling in the dialog box below:
Once you fill it in correctly, you get the same options for stopping execution as in the simpler method below:
Define the variable on one line ending with a semicolon.
Left-click in the area where you set a normal breakpoint and up pops a dialog box.
Choose options to suit your needs.
Any instance where the value of the variable prefDbExists changes, execution will pause as at any "normal" (unconditional) breakpoint. And that's all I needed when I asked the original question.
EDIT: Fully qualified class name is {package name}.{class name})
What you seek for is commonly known as "WatchPoint".
You will find detailed answer and examples on the doc. page of JetBrains - IntelliJ - Idea which is the basis for Android Studio.
https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html
For me this works perfectly !
Add a Kotlin Field Watchpoint by clicking on the space next to the line number for the field declaration in Android Studio. Select Kotlin Field Watchpoint from the Set Breakpoint menu. You will now get a breakpoint whenever that value changes.
Related
Is there any way to have conditional Debug in android studio?
for example I wanna go to debug mode when a specific variable value is.
for example:
for(MyClass myclass: ClassList){
String title = myclass.gettitle(); // this is where break point is
}
Before that I do not need the debug mode. I just wanna examine the app on that exact value. for example when title="KickBoxing" in this case I should check the variable value and if it is not my required value I should press F9 to go to next value. maybe this takes 50 times to get to my desired value.
It is possible to add a condition for a breakpoint. Add a breakpoint, as you normally do, and click with the right mouse button on it. You will be prompt with a dialog. In the condition you can put some java code.
E.g. you could put "kickBoxing".equals(title)
this way the debugger will stop on that breakpoint only when the condition is true.
(photo taken from http://javafortesters.com/)
You can do that with Android Studio:
Set a breakpoint on the line you need.
Right-click on the breakpoint
Paste your condition into the field named Condition
For example, your condition should be like myclass.gettitle().equals("KickBoxing")
What is the shortcut key to find that where is the particular global variable Initializing in android studio project.
Place your cursor on the variable and then press Alt+F7 for the Find Useages command. In the Find Usages result, look for "Value write". Expanding that category will show all the places where the variable is assigned, which includes initialization.
If you middle-mouse-click any of your variables it takes you to the delcaration. If you middle click it again, it pops up a window with every usage. Alt+F7 works too.
In Xcode I am able to "po" any values while debugging. Is there a way to do this in Android Studio?
For example, if I hit a breakpoint and want to print out certain values in realtime as opposed to having to use Logs in my code?
You can do the following steps:
1- Set BreakPoint in the line you want to evaluate Ctrl+F8.
2- Run your code in debug mode or press Shift+F9.
3- When reached to the BreakPoint press Alt+F8 or click on the following button.
4- Then type your value in text-field and click on the Evaluate button.
References:
https://www.jetbrains.com/help/idea/debugging-code.html
https://www.jetbrains.com/help/idea/evaluating-expressions.html
Use Watches. It gives you the ability to evaluate expressions.
When you hit debug point, there are two ways to check values :
(1) Add Watch for any variable
(2) Hover over any variable, the value will be displayed there
If it's a c/c++ project, you still can use lldb command in Android Studio.
Here's the official document. When you hit the breakpoint, you will see a tab named "lldb" next to the variables tab.
In Android Studio, I sometimes have to rename some field, attribute or method name.
I know I must select its name, then hit Alt+Shift+R. Then I type in the new name, and hit Enter.
However, sometimes it works, and sometimes... it just does nothing at all. The new name is just discarded and replaced with the old ones.
Can someone explain me why and how to force this very basic feature to work ?
I suspect that you've missed the refactor preview window that shows up when AS finds some code that it doesn't know if it should be refactored or not. For example, if the method being modified is referenced in comments, then AS will ask if you want to refactor those comment as well.
using a shortcut for "Rename" Variable Shift + F6
or
Select Variable/Method name -> Right Click in java file -> Refactor -> Rename
This also happens when using dagger and trying to refactor via Shift+f6. Android Studio hesitates when renaming things in generated code (which seems silly). Anyway, doing a project clean and then doing the rename works.
Alt+Shift+R replace a selected String (in your case the selected field, attribute...) and doesn't rename the variable, field... itself.
The feature I use for that task is Rename (not Replace). To achieve that you can either use Shift+F6 or right mouse click > Refactor > Rename. Now it changes the name also in other classes (if necessary).
For me, every time I refactor>rename it was showing me the usages and I changed them manually, but the name of the java class stayed the same, even I changed all the usages.
Finally, I unticked all the boxes, and it worked.
Using the latest version of Android Studio.
Once you type the function name and open brackets, AS would automatically display hint with variable type and name (which is really handy). But sometimes it would just disappear.
Does anyone know the keymap (shortcut) that would trigger it to pop again? (haven't found one in AS options, may be I missed one). In particular, I'd like to know the name of the corresponding shortcut so that I can find it through the Preferences screen and update the assigned keys if necessary.
The command name is "Parameter Info".
On Mac, it's assigned to Command+P by default.
On Windows, it's assigned to Ctrl+P by default.
And It's form View menu, called Parameter Info. There is the shortcut on menu.
To display a dropdown version of all the available parameters, you can use Ctrl + Space to show a dropdown list.