Errata

Head First Android Development

Errata for Head First Android Development

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Printed Page 458
The StopwatchFragment code

After completing the chapter's 11 workout app, there was a small mistake when i ran the app on tablet. Here's what happened:
I choose one of the four option and then start running the stopwatch . Then i choose another option and start its stopwatch. Then i press the back button. The first stopwatch now counts 2 seconds per second instead of 1. If i repeat the same steps, it will count 3 seconds per second and so on... I guess the runnables in stopwatchfragment keep counting.
The problem can be fixed by rotating the screen. But that's not a solution. Using a flag in the stopwatchfragment was a simple solution for me. The app was running as it should after that.
Can you please take a look at this problem?
Below are the changes i did in the stopwatchfragment code :

public class StopwatchFragment extends Fragment implements View.OnClickListener{
public int flag ; //created flag that will handle the runnable < ---
...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_stopwatch, container, false);

flag = 1; //set flag = run and then started the runTimer < ---
runTimer(layout);
.....
}

private void runTimer(View view){
......
......
if(running){
seconds++;
}

if(flag == 1) // flag = 1 means that the handler.postdelayed has the right to continue < ---
handler.postDelayed(this,1000); //delay 1000ms (1 second)
}
});


//override onDestroyView() so the specific runnable will not count anymore. This means that the
//previous stopwatch will count normally again (after using the back button).
@Override
public void onDestroyView() {
super.onDestroyView();
flag = 0; //making flag=0 the specific handler.postDelayed will not run anymore
}

















Ian Dale  Oct 07, 2019