0

i have message menu in my action bar and it only shown inside Menu Activity and it will has an alert number for each message that hasn't been read yet, but every time i read my message the number won't change unless i close the application and open it again, how to make it can change the number after i read a message ? i am using back button to open Menu activity after opening message instead opening a new Menu Activity using intent

here is my code to change my action bar alert

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actionbars, menu);
        if (mPrefs.getUserType().equalsIgnoreCase("dosen")){
            menu.findItem( R.id.chgpassword ).setVisible( false );
            menu.findItem( R.id.exit ).setVisible( false );
        } else {

            requestNotif(new ApiCallback(){
                @Override
                public void onSuccess(Integer result){
                    //do stuff here with the list from the request

                    newNotif = result;
                    MenuItem item = menu.findItem(R.id.notif);
                    LayerDrawable icon = (LayerDrawable) item.getIcon();
                    Utils2.setBadgeCount(mContext, icon, newNotif);
                }
            });

        }
        return true;
    }
Togan J.R
  • 99
  • 1
  • 13
  • The `onCreateOptionsMen`u is called when the `Activity` is created. To modify a `MenuItem`, use the `onOptionsItemSelected` method. – Shubham Panchal Apr 03 '20 at 06:28
  • @ShubhamPanchal i need the MenuItem changing without clicking/selecting any option, i need it change when the Menu Activity is resumed – Togan J.R Apr 03 '20 at 06:32

1 Answers1

0

You may call,

invalidateOptionsMenu();

in your Activity. This forces the menu to be recreated and when it is recreated, the method onPrepareOptionsMenu is called,

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem( R.id.notif );
   // Update the item here onwards
}

Refer to this and this answer.

Shubham Panchal
  • 4,061
  • 2
  • 11
  • 36
  • since i am using back button to open Activity i try to call ````invalidateOptionsMenu();```` inside ````onResume()```` but it duplicates my message menu and now showing 2 message menu instead of 1 – Togan J.R Apr 03 '20 at 06:52
  • well it is actually working, but can't be implement inside method ````onResume()```` because it will duplicate my icon menu instead – Togan J.R Apr 03 '20 at 08:04