0

In my Java Android App's MainActivity I have a Toolbar on top (set via setSupportActionBar(Toolbar)) with a youAreloggedIn icon. On starting the app and before it shows or soon after, I want to make a remote connection, check if user is logged in to that remote server and change the icon to indicate that: a lock if not logged in (which is the default) or an opened-lock if user is logged in.

My question is in which callback I should place this code (network enquire and icon change). Right now I have the code at the end of onCreateOptionsMenu() after the menu is inflated.

Is there another onXXX() callback which is more suitable?

Apparently, onStart() is called before the menu is inflated! And so, I can't change icons in there before the menu in the toolbar is created!

Something like onSteadyState() perhaps ?

bliako
  • 977
  • 1
  • 5
  • 16

1 Answers1

1

What you need is invalidateOptionsMenu(), you can call it after the network fetch with can be called asynchronously in onstart()

Read more about this here

bashizip
  • 562
  • 5
  • 14
  • Many Thanks. So, ```onStart()``` is the place and I must do this asynchronously via a callback. My network checks are already asynchronous, so I will make sure that they then call this. This answers my question. But I hoped I also got an answer as to why at the end of ```onStart()``` the menu is not drawn yet! Sure, asynchronicity. But... – bliako Jan 25 '21 at 10:30
  • 1
    Menus are created when OnCreateOptionMenu() is implicitly called at the end of onCreate(); so your menu should've been created even before onStart() ! Can you provide some code so I can better help you? – bashizip Jan 25 '21 at 18:33
  • What I have is an ```androidx.appcompat.widget.Toolbar``` which in ```onCreate()``` is added using ```setSupportActionBar(toolbar);```. Then I overwrite ```public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main, menu); return true;}``` And that's it. The menu is created eventually. Problem is that when I try to change a menu-icon inside ```onStart()``` menu is still null. And so I am forced to do all network checks and subsequent icon-changes in ```OnCreateOptionMenu()```. – bliako Jan 26 '21 at 13:19