How to execute a particular command after clicking an extension's icon in VSCode's activity bar?

In looking at Feature Request: Add an API event to indicate when the sidebar viewlet changes which was closed as waiting for another still open issue on getContext to be resolved. And using intellisense in package.json there is no other key besides id, title and icon. So you cannot put a command entry there for instance (I tried and nothing happened.)

There are a couple of other options that might work for you:

If your views are populated by a TreeView then you can detect when that treeViews' visibility changes:

myTree.onDidChangeVisibility(ev => {
    console.log(ev);                      // ev.visibiltiy = true/false
})

So you can detect when the view becomes visible because the Activity Bar icon is clicked. That does work. However, this event is also triggered when the view is already opened and the user just collapses or expands that individual view. And I don't think there is really any way to distinguish between the two conditions.

If you are populating your views with a WebView there is also an onDidChangeVisibility event for that.


There is an activation event for a View becoming active:

"activationEvents": [
  "onView:package-dependencies"
]

but that will only fire the first time a view is activated - and starts your extension's activate function so is probably not what you are looking for.