Android - openOptionsMenu doesn't work in onCreate

前端 未结 2 476
终归单人心
终归单人心 2020-12-16 19:33

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

new Handler().postDelayed(new Runnable() {
                  


        
相关标签:
2条回答
  • 2020-12-16 20:23

    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.

    0 讨论(0)
  • 2020-12-16 20:23

    My solution

    //Open menu manually from code 
        Timer timing = new Timer();
        timing.schedule(new TimerTask() {
    
                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void run() {
    
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                openOptionsMenu();
                            }
                        });
    
                    }
                }, 1000);
    
    0 讨论(0)
提交回复
热议问题