开发者

Howto extend SomeActivity to relocate e.g. onCreateOptionsMenu?

开发者 https://www.devze.com 2023-01-01 09:36 出处:网络
I know this should be fairly simple but I don\'t get it anyhow. I\'ve got an Ac开发者_高级运维tivity (let\'s call it XyActivity) which has gotten pretty long. Therefore, I\'d like to relocate some ove

I know this should be fairly simple but I don't get it anyhow.

I've got an Ac开发者_高级运维tivity (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


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) {...}
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消