Activity Lifecycle

The threads you create are not automatically aware of the changes in your activity's lifecycle. For example, a thread you spawned would not automatically be notified that your activity's onStop() method has been called, and the activity is not visible anymore, or that your activity's onDestroy() method has been called. This means you may need to do additional work to synchronize your threads with your application's lifecycle. Listing 5–20 shows a simply example of an AsyncTask still running even after the activity has been destroyed.

Listing 5–20. Computing a Fibonacci Number In the Background Thread and Updating the User Interface Accordingly

public class MyActivity extends Activity {     private TextView mResultTextView; ...

Get Pro Android Apps Performance Optimization 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.