views:

218

answers:

2

Hi everyone, I know this should be fairly simple but I don't get it anyhow.
I've got an Activity (let's call it XyActivity) which has gotten pretty long. Therefore, I'd like to relocate some overriden methods to a subclass (let's call it XyOptions). Looks like that:

public class XyActivity extends Activity {
    XyOptions xyOptions;
    public void onCreate(Bundle savedInstanceState) {
        xyOptions = new XyOptions();
        ...
    }
}

and

public class XyOptions extends XyActivity {

    public EditImageOptions() {...}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {...}

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {...}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {...}

Unfortunately, the methods in XyOptions are never called. And I don't get why. I'am sure the answer is trivial so please point me in some direction.

Thanks,
Steff

+1  A: 

You are misunderstanding the inheritance here. If you want to populate the menu in a subclass, try this:

public class XyActivity extends XyOptions {
    public void onCreate(Bundle savedInstanceState) {
       //.. other 
    }
}
public class XyOptions extends Activity {

    public EditImageOptions() {...}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {...}

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {...}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {...}
}
Toni Menzel
You are so damn right. I just figured it out and actually came back to correct myself. This is a little embarrassing. Sorry for my stupidity...
steff
+1  A: 

I think what you are trying to do is, invoke onCreateOptionsMenu in XyOptions while being in the activity XyActivity, by instantiating an object of XyOptions in XyActivity. Its not possible as far I know. onCreateOptionsMenu is only invoked on pressing menu button on the phone.

You should correct the flow of inheritance, as pointed out already.

primalpop