I’m reading the book ‘Hello, Android’. In the Sudoku example, it uses a options menu. It needs a MenuInflater that we use to read the menu definition from XML and turns it into a real view.
To use button, textview, or many other views, I don’t need to inflate them.
My question is, in what situations, I need inflaters? Why doesn’t Android treat menus like other views?
You need an inflater at every place that you want to dynamically create a view out of an XML file.
Activitylayouts are automatically inflated when you callsetContentView()as they’re always required.But when the menu is required — which is only when the user first presses the Menu button — the XML-defined layout needs to be manually inflated.
Similarly, if you have a
ListView, you don’t know in advance what rows will exist, so we have to inflate aViewfrom XML for each row in the list, as they’re required.