Magento – Magento 2 : Store Switcher In admin not working

adminhtmlmagento2store-view

In Magento 2 admin side, I added store switcher in phtml file using this code.

protected function _prepareLayout()
    {
        $this->getToolbar()->addChild(
            'store_switcher',
            'Magento\Backend\Block\Store\Switcher'
        );
        return parent::_prepareLayout();
    }

So above code show store selection in phtml file as below,

enter image description here

When I select diffrent store from the list, then admin URL is change,
but when I access store information in phtml like this $this->_storeManager->getStore()->getCode()

but it always return "default" not selected store code.

Can Any one have idea ?

Best Answer

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="admin-2columns-left">
    <body>
        <referenceContainer name="page.main.actions">
            <block class="Magento\Backend\Block\Store\Switcher" name="store_switcher">
                <action method="setUseConfirm">
                    <argument name="params" xsi:type="string">1</argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>