As of now, I can see UI Theme Detection can be done in one of two ways:
Accessing global variables:
- $User.UITheme – Returns the theme that is supposed to be used.
- $User.UIThemeDisplayed – Returns the theme that is actually being
used.
Calling Apex utility methods:
If users with Lightning Experience permissions are using an unsupported browser, they may experience Salesforce rendered in Salesforce Classic. If this happens, UITheme and UIThemeDisplayed return different values.
Note: Detecting the UI context via a server call is expensive, and should therefore be cached or carefully isolated into init or startup methods(see Apex utility sample below). This will prevent utility being called every time the component is rendered.
If you only need to distinguish between Lightning Experience and Salesforce1, check out $Browser.
{!$Browser.isTablet}
{!$Browser.isPhone}
{!$Browser.isAndroid}
{!$Browser.formFactor}
The $Browser global value provider returns information about the hardware and operating system of the browser accessing the application.
We can also use javascript to find the current user context. Here is the snippet of code that helps you finding the user Experience context
<script>
var j$ = $.noConflict();
j$( document ).ready(function() {
console.log( “ready!11” );
var parentPageUrl = document.referrer;
//alert(parentPageUrl);
if(parentPageUrl.indexOf(“.lightning.force.com”) > 0){
j$(“body”).addClass(‘main-sec-lightning’);
}
else{
j$(“body”).addClass(‘main-sec-classic’);
}
})
</script>
Reference:- How to Share Salesforce Visualforce Pages Between Classic and Lightning Experience?
Introducing UI Theme Detection for Lightning Experience