A Fragment with its own User Interface

The previous app had two separate objects, a Fragment and a View. It was an awkward arrangement because the View’s Path was actually inside the Fragment. The View had to peer into the Fragment to see its own Path.

In this app, the drawing code that used to be in the separate View object has been moved into the Fragment: code follows fields (or as we say in C++, code follows data members.) A fragment is often used to draw one pane of an Activity’s user interface. This Activity’s user interface consists of only one pane.

Source code in Etch.zip

  1. EtchActivity.java: radically simpler.
  2. EtchFragment.java
  3. main.xml: the f in fragment must be lowercase.
  4. AndroidManifest.xml: untouched.

ApiDemos/App/Fragment/Retain Instance

Another example of a Fragment that creates its own user interface with onCreateView.

  1. FragmentRetainInstance.java
  2. fragment_retain_instance.xml