3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ted
  • 19,727
  • 35
  • 96
  • 154
  • As a follow-up question to that: the header, as defined in header.xml, has 4 buttons. Where do I attach the click listeners for those? I do not have a specific Activity for the header.xml. Should I have that? Or should I attach listeners in my Main.java Activity? – Ted Jan 06 '10 at 14:15

3 Answers3

7

You should probably use a separate Activity for each screen; otherwise you need to end up keeping track of which individual View is 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 Activity for each piece of functionality.

If you do decide to keep everything in a single Activity however, you could look at the TabActivity class. However, there are also caveats there that prevent you from having an Activity as the tab content.


Regarding your follow-up, you unfortunately cannot attach an Intent directly to a Button like you can with a MenuItem via the XML, however you could just extend Activity to make your own common base class with some code that hooks up the listeners.

Something like:

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);

        // This needs to be done *after* the View has been inflated
        setupHeaderButtons();
    }
}
Community
  • 1
  • 1
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Thx, do you have any comments on my follow-up question above? Should the header have its on Activity, and can I it as I can with a View? – Ted Jan 06 '10 at 14:22
  • I updated my answer. Activities can't include other activies. But an Activity's layout can include other layouts. You would just include header.xml in each Activity's layout XML using the `` tag. – Christopher Orr Jan 06 '10 at 14:31
  • Thx, for the follow-up answer. I will take a look at it =) – Ted Jan 06 '10 at 14:39
  • I didnt get it to work, and I think I need more help so I posted a new question here, http://stackoverflow.com/questions/2013865/header-views-and-buttons-how-do-i-attach-listeners-to-buttons-in-a-header-th), and referred back to this thread. Thx again =) – Ted Jan 06 '10 at 15:14
2

I am also quite new to Android but my advice would be to create 4 different Activities. The reason for that is that it seems like a "cleaner" implementation to me. Sure, there is more code to be written but I'd rather have more small classes than one big class with lots of code in it.

Stelian Iancu
  • 2,462
  • 3
  • 18
  • 28
  • Thx for the answer =) If you have something nice to say about my follow-up question above, Id appreciate it =) – Ted Jan 06 '10 at 14:23
0

Not sure if this has been mentioned in any of the sub questions, but if you change activities pre-2.0 you cannot animate between them.

So if you have a loading screen and would like it to fade to a menu you have to use two views and switch between the two.

Ljdawson
  • 12,091
  • 11
  • 45
  • 60