tags:

views:

93

answers:

3

Hey, I have touched on this question here, where Christopher gave an answer to this, but I dont really get it so I thought its time to make it a real question, not just a "follow up" =)


As it stands, the application Im writing has 4 different screens: 1. Screen 1 - list of nodes (main screen) 2. Screen 2 - options menu, tableLayout with buttons 3. Screen 3 - navigation 4. 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     |
|                    |
|                    |
|                    |
+--------------------+

The header is just an XML-file (header.xml) with a few buttons. That header.xml is the included in the Layouts using the include-markup. For example, the main.xml has the line:

<include layout="@layout/header"></include>

The header show up alright, but the question is - what is the correct approach to attach OnClickListeners for the buttons in the header?

Christopher pointed out that you could create an Activity class and do the hooks there, like this:

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();
    }
}

First, I cant make it work since the method setupHeaderButtons isnt accessible from FirstActivity. Secondly, is this the right way to go at it?

Regards

+1  A: 

The setupHeaderButtons() method is protected so it can only be accessed by classes that extend that base class, which is BaseActivity in this case.

Are you sure that your FirstActivity is extending BaseActivity?

Christopher
+1 - If setupHeaderButtons() isn't accessible, I bet it's been accidentally declared as private.
Erich Douglass
Thanks for that =) My bad, forgot to extend BaseActivity, hehe.But for static "top menu"/header-stuff as I have above, is this the correct approach? The whole idea I mean, with <include> and all?
Ted
So: For every Activity I create that should have the header on top, i need to have the MyActivity extend the BaseActivity and ALSO run the setupHeaderButtons(). Seems a bit like an "ugly hack"... am I wrong? ---------------------------------------------------------------Also, every time I click the button to change screen, the "startActivity(...)" is executed. Does it know not to create the Activity again, and instead just switch to it (since it is usually already created)??
Ted
Possibly. There's always more than one way to do things (as dtmilano shows in their answer). Every time an Activity comes into view, whether via an `Intent` being fired, or you going back from the previous screen, it goes through the relevant parts of the activity lifecycle.. so always `onResume()` will be called, and perhaps also `onStart()` before that. You need to be wary that Android can kill off anything at any time due to memory requirements and so on.
Christopher
Hi =) The OnClick-listeners call the "startActivity" whenvere pressed, which mean that the onCreate is executed every time I click one of the header buttons. That doesnt feel right at all... What do you say?
Ted
I would be prone to say that's just how things work on Android, but I understand the concern. If you find it actually slows the app down by doing this, then you could post a separate question to ask about how best to handle this kind of rapid-switching behaviour.
Christopher
+1  A: 

I would prefer this so you don't have to remember (and probably forget) to invoke setupHeaderButtons for every derived Activity. BTW, set*U*pHeaderButtons it's a better name.

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

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
            setupHeaderButtons();
    }

    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);
    }
}
dtmilano
Good move overriding `setContentView`, and quite right about my grammar in the method name!
Christopher
+1  A: 

I personally don't think you should overcomplicate things. Having to call setupHeaderButtons should be fine, especially if you only have a handful of activities.

If you're using standard launch modes, the activity will be relaunched. Check out Application Fundamentals if you're interested in learning about launch modes.

James
Thx for that answer. I have now read up on "launchModes", since indeed my app was using startActivity on an Activity. Everytime I clicked the button, a new instance of the Activity was created and added "in top", so it looked like it was being reset, when it really was just "hidden" by the new instance of the Activity.I set "launchMode = singleTask" and now it works like it should =)
Ted