1/* 2 * Copyright (c) 2022 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 */ 15import Ability from '@ohos.app.ability.UIAbility'; 16import commonEvent from '@ohos.commonEvent'; 17 18var TAG1 = 'StageSupportFunction:MainAbility:'; 19var listPush1 = "Stage_SupportFunction_MainAbility_"; 20var status1 = undefined; 21let status2 = undefined; 22var lifeList = []; 23export default class MainAbility extends Ability { 24 25 onCreate(want, launchParam) { 26 console.log(TAG1 + 'onCreate : status1 : ' + status1 + ' ,ifeList : ' + JSON.stringify(lifeList)); 27 status1 = this.context.isTerminating(); 28 lifeList.push('onCreate'); 29 30 commonEvent.publish(listPush1 + "onCreate", (err) => { 31 console.log(TAG1 + listPush1 + "onCreate"); 32 }); 33 34 setTimeout(() => { 35 if (want.parameters.number == 1) { 36 this.context.terminateSelf().then((data) => { 37 console.log(TAG1 + "terminateSelfWithResult data = " + JSON.stringify(data)); 38 }).catch((error) => { 39 console.log(TAG1 + "terminateSelfWithResult error = " + JSON.stringify(error)); 40 }) 41 } 42 if (want.parameters.number == 2) { 43 let wantNum = { 44 want: { 45 bundleName: "ohos.acts.aafwk.test.stagesupportfunction", 46 abilityName:"MainAbility" 47 }, 48 resultCode: 12120 49 } 50 this.context.terminateSelfWithResult(wantNum).then((data) => { 51 console.log(TAG1 + "terminateSelfWithResult data = " + JSON.stringify(data)); 52 }).catch((error) => { 53 console.log(TAG1 + "terminateSelfWithResult error = " + JSON.stringify(error)); 54 }) 55 } 56 }, 2000); 57 } 58 59 onDestroy() { 60 console.log(TAG1 + 'onDestroy'); 61 lifeList.push('onDestroy'); 62 status2 = this.context.isTerminating(); 63 let options = { 64 parameters: { 65 isTerminating1: status1, 66 isTerminating2: status2, 67 lifeList: lifeList 68 } 69 } 70 commonEvent.publish(listPush1 + "onDestroy", options, (err) => { 71 console.log(TAG1 + listPush1 + "onDestroy"); 72 }); 73 } 74 75 onWindowStageCreate(windowStage) { 76 console.log(TAG1 + 'onWindowStageCreate'); 77 lifeList.push('onWindowStageCreate'); 78 79 windowStage.loadContent("pages/index", (err, data) => { 80 if (err.code) { 81 console.log(TAG1 + 'Failed to load the content. Cause:' + JSON.stringify(err)); 82 return; 83 } 84 console.log(TAG1 + 'Succeeded in loading the content. Data: ' + JSON.stringify(data)); 85 }); 86 } 87 88 onWindowStageDestroy() { 89 console.log(TAG1 + 'onWindowStageDestroy'); 90 } 91 92 onForeground() { 93 console.log(TAG1 + 'onForeground'); 94 lifeList.push('onForeground'); 95 } 96 97 onBackground() { 98 console.log(TAG1 + 'onBackground'); 99 } 100 101 onBackPressed(): boolean { 102 console.log(TAG1 + 'onBackPressed'); 103 return false; 104 } 105}; 106