Reputation: 3953
I am working on Android application in which I want to refresh my menu at run time. For example if in the starting my menu is like menu1.xml
then after getting any result for example 'Yes' from the server I want to refresh it with menu2.xml
.
I want to use invalidate function for this but I am not able to make my menu items to be refreshed. My code is given below:
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
if (showAcceptButton==true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}if (Constants.showAcceptedButton = true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2, menu);
return true;
}
if(showNormalMenu==true){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
return true;
}
At onCreate for the first time it loads the menu but on run time I want to load another menu items.
Upvotes: 0
Views: 516
Reputation: 2757
Once try as follows
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
It will give existing menu object and you can add or delete menu items from it.
UPDATE:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.clear();
if (showAcceptButton==true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}if (Constants.showAcceptedButton = true) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2, menu);
return true;
}
if(showNormalMenu==true){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
return super.onPrepareOptionsMenu(menu);
}
Hope this will helps you.
Upvotes: 1