当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to call for a method from OnCreate with menu item?

问题描述:

I'm building an app with option to load a work space from remote (via ADB), using this code in OnCreate:

Bundle extras = this.getIntent ( ).getExtras ( );

if (extras != null && extras.containsKey("workspace"))

{

String param = extras.getString("workspace");

WSmethods.loadWorkspace(param); //work space methods class

}

This work space effect save menu item, therefore my workspace constractor contain this menu item, like this:

public workspaceMethods(MyActivity myActivity, MenuItem saveItem)

but, in order to receive MenuItem I have to wait for onCreateOptionsMenu method that called after OnCreate

Therefore, I found myself in a tangle: On the one hand I have to call my work space methods in OnCreate (in order to receive commands from ADB) and on the other hand I have to receive my menu item that initialize after OnCreate called.

Bottom line, the question: How can I call for method from OnCreate with a menu item? (to be more accurate, in my case the question is how to initialize class variable with menu item in OnCreate method?)

EDIT I initialize WSmethods like this (in onCreateOptionsMenu):

WSmethods = new workspaceMethods(this, menu.findItem(R.id.action_save));

网友答案:

You have all access to intent extras even from onCreateOptionsMenu. So just call your method from it - after inflating your menu.

网友答案:

EDIT:

I'm just going to restart as I was under the impression you were implementing the fragment framework.

try this:

@Override
public void onPostCreate(Bundle savedInstance)
{
    super .onPostCreate(savedInstance);
    WSmethods = new workspaceMethods(this, menu.findItem(R.id.action_save));
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();
    switch (id)
    {
        case R.id.action_save:
             WSmethods = new workspaceMethods(Activity.this, item);
             //do actions associated with method
              break;
             //other switch cases
         default:
           //implement a default action
              break;
     }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: