Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - openOptionsMenu doesn't work in onCreate

Tags:

android

Is there any other way to call openOptionsMenu after activity is displayed without using something like this:

new Handler().postDelayed(new Runnable() {
            public void run() {
                openOptionsMenu();
            }
        }, 1000); 

Reference: http://groups.google.com/group/android-beginners/browse_frm/thread/b10a8ea840c07725/1ce48bb147a3ed1a?#1ce48bb147a3ed1a

EDIT: I would appreciate example like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Now I guess something like Window.Callback.onAttachedToWindow(...) should be done?
}
like image 370
nikib3ro Avatar asked May 05 '10 22:05

nikib3ro


1 Answers

I looked at Activity again, and it has had the method onAttachedToWindow, inherited from Window.Callback, since API level 5. If you are using this level, then you simply have to override this method in your Activity.

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    openOptionsMenu();
}

If you are using a version prior to 5, then you have to override the onAttachedToWindow method in View instead. This is very easy if your View is created in code. If it is created in XMl, then it isn't that much harder - you should find the instructions here helpful.

like image 61
Casebash Avatar answered Nov 18 '22 04:11

Casebash