 
     if KD.isLoggedIn()
       @emit 'AppManagerWantsToShowAnApp', appInstance, appView, appOptions
+      @setLastActiveIndex appInstance
       callback? appInstance
     else
       KD.getSingleton('router').handleRoute '/', replaceState: yes


    if KD.isLoggedIn()
      @emit 'AppManagerWantsToShowAnApp', appInstance, appView, appOptions
      callback? appInstance
    else
      KD.getSingleton('router').handleRoute '/', replaceState: yes

    @unregister appInstance
    callback()

  get:(name)->

    if apps = @appControllers[name]
      apps.instances[apps.lastActiveIndex] or apps.instances.first
    else
      null

  getByView: (view)->

    appInstance = null
    for name, apps of @appControllers
      for appController in apps.instances
        if view.getId() is appController.getView?()?.getId()
          appInstance = appController
          break
      break if appInstance

    return appInstance

  getFrontApp:-> @frontApp

  setFrontApp:(appInstance)->

    @setLastActiveIndex appInstance
    @frontApp = appInstance

  register:(appInstance)->

    name = appInstance.getOption "name"
    @appControllers[name] ?=
      instances       : []
      lastActiveIndex : null

    @appControllers[name].instances.push appInstance
    @setListeners appInstance

  unregister:(appInstance)->

    name  = appInstance.getOption "name"
    index = @appControllers[name].instances.indexOf appInstance

    if index >= 0
      @appControllers[name].instances.splice index, 1
      if @appControllers[name].instances.length is 0
        delete @appControllers[name]
      appInstance.destroy()

