diff --git a/frameworks/native/ability/native/ability_impl.cpp b/frameworks/native/ability/native/ability_impl.cpp index 2822e357ea..e60cd43768 100644 --- a/frameworks/native/ability/native/ability_impl.cpp +++ b/frameworks/native/ability/native/ability_impl.cpp @@ -596,7 +596,29 @@ void AbilityImpl::WindowLifeCycleImpl::AfterForeground() { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); HILOG_DEBUG("%{public}s begin.", __func__); - AbilityTransitionDone(AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW); + auto owner = owner_.lock(); + if (owner == nullptr || !owner->IsStageBasedModel()) { + HILOG_ERROR("Not stage mode ability or abilityImpl is nullptr."); + return; + } + bool needNotifyAMS = false; + { + std::lock_guard lock(owner->notifyForegroundLock_); + if (owner->notifyForegroundByAbility_) { + owner->notifyForegroundByAbility_ = false; + needNotifyAMS = true; + } else { + HILOG_DEBUG("Notify foreground invalid mode by window, but client's foreground is running."); + owner->notifyForegroundByWindow_ = true; + } + } + + if (needNotifyAMS) { + HILOG_DEBUG("Stage mode ability, window after foreground, notify ability manager service."); + PacMap restoreData; + AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, + AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW, restoreData); + } } void AbilityImpl::WindowLifeCycleImpl::AfterBackground() @@ -637,75 +659,24 @@ void AbilityImpl::WindowLifeCycleImpl::AfterUnfocused() void AbilityImpl::WindowLifeCycleImpl::ForegroundFailed() { HILOG_DEBUG("%{public}s begin.", __func__); - auto owner = owner_.lock(); - if (owner == nullptr) { - HILOG_ERROR("abilityImpl is nullptr."); - return; - } - - auto ability = owner->ability_; - if (ability == nullptr) { - HILOG_ERROR("jsAbility is nullptr."); - return; - } - - bool needNotifyAMS = false; - { - std::lock_guard lock(owner->notifyForegroundLock_); - if (owner->notifyForegroundByAbility_) { - owner->notifyForegroundByAbility_ = false; - needNotifyAMS = true; - } else { - HILOG_DEBUG("Notify foreground failed by window, but client's foreground is running."); - owner->notifyForegroundByWindow_ = true; - } - } - - if (needNotifyAMS) { - HILOG_DEBUG("The ability is stage mode, schedule foreground failed."); - PacMap restoreData; - AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, - AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_FAILED, restoreData); - } + PacMap restoreData; + AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, + AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_FAILED, restoreData); } void AbilityImpl::WindowLifeCycleImpl::ForegroundInvalidMode() { HILOG_DEBUG("%{public}s begin.", __func__); - AbilityTransitionDone(AbilityLifeCycleState::ABILITY_STATE_INVALID_WINDOW_MODE); -} - -void AbilityImpl::WindowLifeCycleImpl::AbilityTransitionDone(AbilityLifeCycleState state) const -{ auto owner = owner_.lock(); if (owner == nullptr || !owner->IsStageBasedModel()) { HILOG_ERROR("Not stage mode ability or abilityImpl is nullptr."); return; } - auto ability = owner->ability_; - if (ability == nullptr) { - HILOG_ERROR("jsAbility is nullptr."); - return; - } - - bool needNotifyAMS = false; - { - std::lock_guard lock(owner->notifyForegroundLock_); - if (owner->notifyForegroundByAbility_) { - owner->notifyForegroundByAbility_ = false; - needNotifyAMS = true; - } else { - HILOG_DEBUG("Notify foreground invalid mode by window, but client's foreground is running."); - owner->notifyForegroundByWindow_ = true; - } - } - - if (needNotifyAMS) { - HILOG_DEBUG("The ability is stage mode, schedule %{public}u.", static_cast(state)); - PacMap restoreData; - AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, state, restoreData); - } + HILOG_DEBUG("The ability is stage mode, schedule foreground invalid mode."); + PacMap restoreData; + AbilityManagerClient::GetInstance()->AbilityTransitionDone(token_, + AbilityLifeCycleState::ABILITY_STATE_INVALID_WINDOW_MODE, restoreData); } void AbilityImpl::Foreground(const Want &want) diff --git a/frameworks/native/ability/native/new_ability_impl.cpp b/frameworks/native/ability/native/new_ability_impl.cpp index 18ad456c4a..02b875412a 100644 --- a/frameworks/native/ability/native/new_ability_impl.cpp +++ b/frameworks/native/ability/native/new_ability_impl.cpp @@ -112,8 +112,8 @@ bool NewAbilityImpl::AbilityTransaction(const Want &want, const AAFwk::LifeCycle Foreground(want); std::lock_guard lock(notifyForegroundLock_); ret = notifyForegroundByWindow_; - notifyForegroundByWindow_ = false; if (ret) { + notifyForegroundByWindow_ = false; notifyForegroundByAbility_ = false; } } diff --git a/interfaces/kits/native/ability/native/ability_impl.h b/interfaces/kits/native/ability/native/ability_impl.h index 93f915b62e..3fa0b7ee50 100644 --- a/interfaces/kits/native/ability/native/ability_impl.h +++ b/interfaces/kits/native/ability/native/ability_impl.h @@ -494,7 +494,6 @@ public: void ForegroundFailed() override; void ForegroundInvalidMode() override; private: - void AbilityTransitionDone(AbilityLifeCycleState state) const; sptr token_ = nullptr; std::weak_ptr owner_; };