6.7. CannonGameFragment Subclass of Fragment

Class CannonGameFragment (Fig. 6.6) overrides four Fragment methods:

onCreateView (lines 17–28)—As you learned in Section 5.3.3, this method is called after a Fragment’s onCreate method to build and return a View containing the Fragment’s GUI. Lines 22–23 inflate the GUI. Line 26 gets a reference to the CannonGameFragment’s CannonView so that we can call its methods.

onActivityCreated (lines 31–38)—This method is called after the Fragment’s host Activity is created. Line 37 calls the Activity’s setVolumeControlStream method to allow the game’s audio volume to be controlled by the device’s volume keys.

onPause (lines 41–46)—When the MainActivity is sent to the background (and thus, paused), the ...

Get Android™ How to Program, Second Edition 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.