Ahmet K
Ahmet K

Reputation: 813

Android Studio Debugger : Stop when an variable gets a specific value

Scenario : I have a variable and I want to find out where and when the variable gets for example the value 4. When this happens the debugger should stop at that line.

Is this possible with Android Studio ?

Upvotes: 1

Views: 1712

Answers (2)

Slartibartfast
Slartibartfast

Reputation: 611

If I understand correctly what you're wanting to do is set what is called a "Watchpoint" in Android Studio, and here's a link to the IntelliJ documentation that discusses them:

https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html

In particular, what you want to do is set a break-point on the member variable itself, right-click that break-point and set the watch on "field modification" and set a condition for when that variable becomes the specific value you're trying to find.

So, in this simple bit of code:

public class MainActivity extends AppCompatActivity
{

int mWatchMe = -10;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i=0 ; i<20 ; i++)
        {
            mWatchMe++;
        }
    }
}

You would set a break-point on the line: int mWatchMe = -10;.

Set the "Condition" and "modification" fields:
enter image description here

The execution should break where ever "mWatchMe" is set to '0':
enter image description here

Upvotes: 7

mrkernelpanic
mrkernelpanic

Reputation: 4451

This is possible. Right click on your breakpoint and then enter your expression "value == 4" in the condition field.

Upvotes: 2

Related Questions