Tackling the infinite loop situation

In our preceding code, there was a chance of running into an infinite loop and so we replace our retry and add in a counter for extra safety in case an infinite loop situation arises in our surfaceDestroyed() method. It is possible that our retry might return a false value or a true value during every run, and so there's a chance of an infinite loop situation. To avoid this, we have this counter which increments every time the while loop is running and stops after that. You can go ahead and experiment to see what problems occur if you avoid using the counter for yourself:

@Overridepublic void surfaceDestroyed(SurfaceHolder holder){    boolean retry = true;    int counter = 0;    while(retry && counter <1000)

Get Learning Android Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.