Handling items from an Action Bar outside the onCreateOptionsMenu()

Asked

Viewed 189 times

6

I have an item in my Action Bar to display progress (a Progressbar) if a request is occurring. When the request occurs, I must show Progressbar and hide the other items, when the request ends, I must hide Progressbar and show the other items.

Diculdade

The request, ie my Asynctask, is executed when starting my Activity (actually it is a Fragment), ie before the operating system calls the creation of the menu with onCreateOptionsMenu().

I need to retrieve and manipulate the items of a Action Bar outside the scope of the method onCreateOptionsMenu(), that is at the beginning and end of Asynctask.

Observing

Asynctask is declassified into a class by class. Activity delegates to a class that manipulates and manages the methods of this Asynctask.

How to handle an Actionbar outside the scope of onCreateOptionsMenu()?

  • You can explain better what you want to do with the menu after obtained. Thank you.

  • Eduardo, I need to hide/show. I’ll go into the question.

  • At what point do you intend to do this, you cannot use the onCreateOptionsMenu event().

  • After running an asynctask. The item I want to hide is a progressbar. Show it during the request and hide it and display other items when completed the request.

  • You can take the Menu at the event onCreateOptionsMenu() and pass by parameter to your AsyncTask. What do you think of the idea ?

  • My asynctask runs on onStart(), so before onCreateOptionsMenu() happens.

  • You can’t change the method in which your AsyncTask is executed ?

  • Your Asynctask is declared in Activity where it is used (nested class) or is a separate class?

  • In a separate class, @ramaral. Activity delegates to another class that contains Asynctask.

Show 4 more comments

3 answers

2

You can’t manipulate your Menu before he was even created.

The way I believe it is most correct is for you to perform your AsyncTaskafter the method onCreateOptionsMenu() be executed, and pass the Menu for your AsyncTask per parameter.

2


It is not possible to manipulate something before it has been created.
So you’ll only have access to Menu after the method onCreateOptionsMenu() have been executed.

I suggest you do the following:

  • Declare a private variable to save the reference to Menu: mMenu.
  • Declare a method to execute the Asynctask.
  • In the method onCreateOptionsMenu() check whether mMenu is null if the reference to the menu is stored in it and calls the method that executes the Asynctask.
  • In the method onStart() check whether mMenu is void if nay for call the method that executes the Asynctask

So when the Activity is created, the Asynctask is launched in the method onCreateOptionsMenu(), in other situations arising from the normal life cycle of Activity, to Asynctask is launched in the method onStart().

You must pass on the reference to Menu to his Asynctask.

0

You can use the method invalideOptionsMenu()

That will force the method call onCreateOptionsMenu()

  • Caique, how would you use your approach? You can provide an example?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.