Mak
Mak

Reputation: 543

Callback when the options menu creation is complete

I need to make sure that the options menu has been created before i run a certain code that accesses one of the menu items. Is there a callback for that or how can i implement one?

Is there any guaranties that the menu has been created on the activity's onResume()?

Upvotes: 29

Views: 2785

Answers (4)

Sourav Dey
Sourav Dey

Reputation: 34

In Android, it is not possible to directly determine if an options menu has been created from within the onResume() method. This is because the onCreateOptionsMenu() method, which is responsible for creating the options menu, is usually called before onResume(). Nevertheless, there is a way to accomplish this by implementing a callback mechanism.

public class YourActivity extends AppCompatActivity {
private boolean isOptionsMenuCreated = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.your_menu, menu);
    isOptionsMenuCreated = true; // Set the flag to true when the menu is created
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    
    // Check if the options menu has been created before accessing the menu item
    if (isOptionsMenuCreated) {
        // Your code to access the menu item goes here
        MenuItem menuItem = findViewById(R.id.your_menu_item);
        // Do something with menuItem
    }
}

}

Upvotes: 0

zaifrun
zaifrun

Reputation: 941

You cannot use onResume for this.

In fact this is the call order when your app is launched :

  1. onResume()
  2. onCreateOptionMenu
  3. onPrepareOptionsMenu

As you can see onCreateOptionsMenu is called AFTER onResume (you can verify this yourself by writing to the console in the overriden methods). Also notice that onPrepareOptionsMenu is called at least once - when your app is starting, even though the menu is at that time not shown to the user.

Now, you do not write in great detail about what you are trying to do, but if what you want to do is this "run a certain code that accesses one of the menu items", then you can use onPrepareOptionsMenu as it is called AFTER onCreateOptionsMenu and it also called whenever your app comes to the foreground - i.e. after onResume. Depending on whether this code needs to run once or everytime you can use a boolean flag inside that method (or store it in the Preferences or similiar persistent data if it is only once ever).

There are no other callback hooks mentioned in the documentation and I have never had the need for others, as onPrepareOptionsMenu should be enough to do the job. If you feel this is not the case, you need to be more specific in your answer and provide code for your specific usecase.

But as I said before, no other callbacks are mentioned in the documentation.

Upvotes: 0

gleenn
gleenn

Reputation: 628

It is not a callback for after onCreateOptionsMenu is done, but onPrepareOptionsMenu can be used if you want to modify the menu before it is displayed to make modifications. This would only be called after onCreateOptionsMenu (if Android is behaving, which is should).

http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)

Upvotes: -2

Related Questions