Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break on variable value change in Android Studio 1.1.0?

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?

like image 270
DSlomer64 Avatar asked May 13 '15 23:05

DSlomer64


People also ask

What is break point in Android Studio?

The Breakpoints window lets you enable or disable each breakpoint from the list on the left. If a breakpoint is disabled, Android Studio does not pause your app when it hits that breakpoint. Select a breakpoint from the list to configure its settings.

How do you break a variable change in Visual Studio?

Setting a data breakpoint is as easy as right-clicking on the property you're interested in watching inside the watch, autos, or locals window and selecting “Break when value changes” in the context menu. All data breakpoints are displayed in the Breakpoints window.


2 Answers

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.

like image 64
Hrym Avatar answered Sep 21 '22 15:09

Hrym


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:

enter image description here

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:

enter image description here

Once you fill it in correctly, you get the same options for stopping execution as in the simpler method below:

  1. Define the variable on one line ending with a semicolon.
  2. Left-click in the area where you set a normal breakpoint and up pops a dialog box. enter image description here
  3. 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})

like image 35
DSlomer64 Avatar answered Sep 21 '22 15:09

DSlomer64