I've been trying to use the TabHost. If you look it up at the Android API Reference, the entry on any of the associated Views (TabHost or TabWidget) send you to this tutorial. It begins by briefly mentioning that you can use the Tab set up to flip between views, or entire activities. Having said this, it chooses the latter, and steps you through a process that includes creating separate Activities and layout files for each tab.
Part way through this process, I found myself wondering why it's such an involved process. With so much Android UI defined by xml files, why do I have to create all these classes and then hook them up programmatically?
So I went looking for another example. A Google search for tab demos revealed an alternate example. Especially surprising is that this simpler example is also on the Android Developer site. In this approach, you lay out all the tab content in one xml file, and don't have to create any activities for each tab. Though you still have to create the tabs and connect them to their content in code, which seems un-Androidy. But overall it's a more straightforward approach if your tab set-up is relatively simple.
Really, I don't know why they link you to the more complex tutorial, but now you know there's an alternative. Also, whichever approach you use, here's some advice: When you put the TabHost and the FrameLayout in a LinearLayout, remember to set the orientation to vertical. It would be really embarrassing if you got that wrong and then couldn't figure out where the content went. I'm guessing.
Tuesday, October 25, 2011
Wednesday, October 12, 2011
Activity Constructor vs. onCreate
This isn't explicitly stated in many tutorials, but something I've learned from experience. The onCreate method in Activities is called when the Activity starts, and books show all the set-up work being done in that method. But what about the Activity constructor? Can you use it for any initialisation work?
No. In general you should leave it alone, as much of the Android API will not work if called from the Activity constructor. For instance, trying to inflate a layout will raise an exception. So restrict your initialisation to onCreate.
No. In general you should leave it alone, as much of the Android API will not work if called from the Activity constructor. For instance, trying to inflate a layout will raise an exception. So restrict your initialisation to onCreate.
Subscribe to:
Comments (Atom)