Since I am new to Android, I am now thinking on what is the correct way of doing things.
As it stands, the application I’m writing has 4 different screens:
- Screen 1 – list of nodes (main screen)
- Screen 2 – options menu, tableLayout with buttons
- Screen 3 – navigation
- Screen 4 – text details on version etc
These screens can be navigated to/from using a “header” View that is placed on top. The header then has 4 different buttons:
+--------------------+
| menu with buttons |
+--------------------+
| |
| |
| |
| C O N T E N T |
| |
| |
| |
+--------------------+
main.xml is really just a LinearLayout that INCLUDES the header.xml and then the content, in that case the list of nodes in a ListView
options.xml is the same thing almost, it includes the headerxml and then a bunch of buttons…
…and so on with the two other screens.
So, when I press one of the buttons in the header/menu on top the content should be switched to that screen. My question is:
-
Should I create one Activity for each screen? I read on Google that:
An activity presents a visual user interface for one focused endeavor the user can undertake. So that can be interpreted that I should use one Activity for each of these screens. -
Should I not create more Activities than the startup, and then just run the setContentView(R.layout.whatever) when I want to change the “content” above?
You should probably use a separate
Activityfor each screen; otherwise you need to end up keeping track of which individualViewis currently being displayed, plus the state of all those not currently being displayed when the user switches to another window, or a call comes in etc.It’s easier to keep track of this state if you just use a separate
Activityfor each piece of functionality.If you do decide to keep everything in a single
Activityhowever, you could look at theTabActivityclass. However, there are also caveats there that prevent you from having anActivityas the tab content.Regarding your follow-up, you unfortunately cannot attach an
Intentdirectly to aButtonlike you can with aMenuItemvia the XML, however you could just extendActivityto make your own common base class with some code that hooks up the listeners.Something like: