How do I programmatically change the admin theme? How do I define a different administration theme for a user and administration role in Drupal 8?

I have looked at the context module for Drupal 8. There is a problem with the context condition and the reaction theme related items are all based on the front-end themes. I need the same but for the back-end themes.

My code is the same as the context reaction code for Theme.php. I just changed the submitConfiguration().

 /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &amp;$form, FormStateInterface $form_state) {
    $configuration['admin'] = $form_state-&gt;getValue('theme');
    $configuration += $this-&gt;getConfiguration();
      \Drupal::configFactory()
          -&gt;getEditable('system.theme')
          -&gt;set('admin', $form_state-&gt;getValue('theme'))
          -&gt;save();
    $this-&gt;setConfiguration($configuration);
  }


I defined the following in the context configuration.


Condition => Role:User

Reaction => Admin Theme: Customtheme 
Condition => Role: Administrator 

Reaction => Admin Theme: Seven


Is this possible? Are there other methods for achieving this goal?
As the context module is still in alpha I would try a solution in custom code by extending the class AdminNegotiator:

src/Theme/CustomAdminNegotiator.php

&lt;?php

namespace Drupal\mymodule\Theme;

use Drupal\user\Theme\AdminNegotiator;
use Drupal\Core\Routing\RouteMatchInterface;

class CustomAdminNegotiator extends AdminNegotiator {

  public function determineActiveTheme(RouteMatchInterface $route_match) {
    if (in_array('custom_role', $this-&gt;user-&gt;getRoles())) {
      return 'custom_admin_theme';
    }
  }

}


and register this service class in mymodule.services.yml tagged as theme negotiator with a priority higher than the default admin negotiator (-40):

services:
  theme.negotiator.admin_theme.custom:
    class: Drupal\mymodule\Theme\CustomAdminNegotiator
    arguments: ['@current_user', '@config.factory', '@entity.manager', '@router.admin_context']
    tags:
      - { name: theme_negotiator, priority: -39 }


The applies() method is kept the same as in AdminNegotiator, so that only users with the correct permission have access to this theme and this only applies for admin routes.