1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_APPEXECFWK_OHOS_LIFECYCLE_OBSERVER_INTERFACE_H 17 #define FOUNDATION_APPEXECFWK_OHOS_LIFECYCLE_OBSERVER_INTERFACE_H 18 19 #include "ability_lifecycle_observer_interface.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { 23 using Want = OHOS::AAFwk::Want; 24 class LifecycleObserver : public ILifecycleObserver { 25 public: 26 LifecycleObserver() = default; 27 virtual ~LifecycleObserver() = default; 28 29 /** 30 * @brief Called back in response to an ON_ACTIVE event. 31 * When an ON_ACTIVE event is received, the ability or ability slice is in the foreground and is interactive. 32 */ 33 virtual void OnActive(); 34 35 /** 36 * @brief Called back in response to an ON_BACKGROUND event. 37 * When an ON_BACKGROUND event is received, the ability or ability slice is invisible. You are advised to 38 * suspend the threads related to this ability or ability slice and clear resources for more system memory. 39 * 40 */ 41 virtual void OnBackground(); 42 43 /** 44 * @brief Called back in response to an ON_FOREGROUND event, where information for the 45 * ability or ability slice to go back to the ACTIVE state is carried in the want parameter. 46 * When an ON_FOREGROUND event is received, the ability or ability slice returns to the foreground. You can use 47 * this method to implement re-initialization or adjust the UI display by using the want parameter. 48 * 49 * @param want Indicates the information for the ability or ability slice to go back to the ACTIVE state. 50 */ 51 virtual void OnForeground(const Want &want); 52 53 /** 54 * @brief Called back in response to an ON_INACTIVE event. 55 * When an ON_INACTIVE event is received, the ability or ability slice is in the INACTIVE state. INACTIVE is an 56 * intermediate state before the state changes to ACTIVE or BACKGROUND. In this state, the UI may be visible but is 57 * not interactive. You are advised not to use this method to invoke complex service logic. 58 * 59 */ 60 virtual void OnInactive(); 61 62 /** 63 * @brief Called back in response to an ON_START event, where the startup information 64 * is carried in the want parameter. 65 * This method initializes an Ability or AbilitySlice and is called back only once during the entire lifecycle. 66 * You are advised to implement some initialization logic using this method, for example, you can initialize a 67 * timer or define some global objects. 68 * 69 * @param want Indicates the startup information. 70 */ 71 virtual void OnStart(const Want &want); 72 73 /** 74 * @brief Called back in response to an ON_STOP event. 75 * This method is called back when the lifecycle of the ability or ability slice is destroyed. You can reclaim 76 * resources using this method. 77 * 78 */ 79 virtual void OnStop(); 80 81 /** 82 * @brief Called back in response to a lifecycle change. This method is triggered by a registered LifecycleObserver 83 * each time the lifecycle state changes. 84 * 85 * @param event Indicates the lifecycle event. 86 * @param want Indicates the state change information. 87 */ 88 virtual void OnStateChanged(Lifecycle::Event event, const Want &want); 89 90 /** 91 * @brief Called back in response to a lifecycle change. This method is triggered by a registered LifecycleObserver 92 * each time the lifecycle state changes. 93 * 94 * @param event Indicates the lifecycle event. 95 */ 96 virtual void OnStateChanged(LifeCycle::Event event); 97 }; 98 } // namespace AppExecFwk 99 } // namespace OHOS 100 #endif // FOUNDATION_APPEXECFWK_OHOS_LIFECYCLE_OBSERVER_INTERFACE_H