UserAppInfo Entity in Salesforce stores the last Lightning app logged in to by the user. So, we can get the DurableId from it and use it for querying the App details from AppDefinition entity.
Sample Code:
UserAppInfo objUserAppInfo = [
SELECT Id, AppDefinitionId, UserId, User.Name
FROM UserAppInfo
WHERE UserId =: UserInfo.getUserId()
];
AppDefinition objAppDefinition = [
SELECT Label FROM AppDefinition
WHERE DurableId =: objUserAppInfo.AppDefinitionId
];
System.debug(
'App is ' +
objAppDefinition.Label
);
Output: