# UTSActivityCallback 为了更好的对外暴露activity的方法,我们把activity里面包含的方法按照继承关系进行了分类,下面是分类标准 第一类:activity生命周期相关方法[UniActivityLifeCycleCallback](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#UniActivityLifeCycleCallback)\ 比如 oncreate,ondestory 等\ 第二类: 键盘事件相关方法[UniActivityLifeCycleCallback](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#UniActivityLifeCycleCallback)\ 比如 onKeyDown,onKeyUp等\ 第三类:windon窗体的相关方法[UniActivityLifeCycleCallback](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#UniActivityLifeCycleCallback)\ 比如onCreatePanelMenu,onWindowDismissed等\ 第四类:activity本身自带的相关方法,不继承自其他类[UniActivityLifeCycleCallback](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#UniActivityLifeCycleCallback)\ 比如onProvideAssistData等\ 第五类:Component 组件相关方法[UniActivityLifeCycleCallback](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#UniActivityLifeCycleCallback)\ 比如onTrimMemory等 并且上面的五个类都为IUniActivityCallback的实现类,我们在使用的时候可以传入具体的实现类,然后按照具体需求重写其中的某个方法,具体用法参考[示例](https://doc.dcloud.net.cn/uni-app-x/uts/utsactivitycallback.html#示例) ## UniActivityCallback ### 实例方法 #### onAttachFragment(params, fragment) #### onUserInteraction(params) #### onUserLeaveHint(params) #### onActivityResult(params, requestCode, resultCode, data) #### onRequestPermissionsResult(params, requestCode, permissions, grantResults) #### onApplyThemeResource(params, theme, resid, first) #### onCreateView(params, parent, name, context, attrs) #### onTitleChanged(params, title, color) #### onChildTitleChanged(params, childActivity, title) #### onBackPressed(params) #### onContextMenuClosed(params, menu) #### onCreateContextMenu(params, menu, v, menuInfo) #### onOptionsMenuClosed(params, menu) #### onPrepareNavigateUpTaskStack(params, builder) #### onProvideAssistData(params, data) #### onProvideAssistContent(params, outContent) #### onPreAttachFragment(params, fragment) #### onPreUserInteraction(params) #### onPreUserLeaveHint(params) #### onPreActivityResult(params, requestCode, resultCode, data) #### onPreRequestPermissionsResult(params, requestCode, permissions, grantResults) #### onPreApplyThemeResource(params, theme, resid, first) #### onPreCreateView(params, parent, name, context, attrs) #### onPreTitleChanged(params, title, color) #### onPreChildTitleChanged(params, childActivity, title) #### onPreBackPressed(params) #### onPreContextMenuClosed(params, menu) #### onPreCreateContextMenu(params, menu, v, menuInfo) #### onPreOptionsMenuClosed(params, menu) #### onPrePrepareNavigateUpTaskStack(params, builder) #### onPreProvideAssistData(params, data) #### onPreProvideAssistContent(params, outContent) ## UniActivityComponentCallback ### 实例方法 #### onConfigurationChanged(params, newConfig) #### onLowMemory(params) #### onTrimMemory(params, level) #### onPreConfigurationChanged(params, newConfig) #### onPreLowMemory(params) #### onPreTrimMemory(params, level) ## UniActivityKeyEventCallback ### 实例方法 #### onKeyDown(params, keyCode, event) #### onKeyLongPress(params, keyCode, event) #### onKeyUp(params, keyCode, event) #### onKeyMultiple(params, keyCode, repeatCount, event) #### onPreKeyDown(params, keyCode, event) #### onPreKeyLongPress(params, keyCode, event) #### onPreKeyUp(params, keyCode, event) #### onPreKeyMultiple(params, keyCode, repeatCount, event) ## UniActivityLifeCycleCallback ### 实例方法 #### onCreate(params, savedInstanceState) #### onPreCreate(params, savedInstanceState) #### onStart(params) #### onPreStart(params) #### onRestart(params) #### onPreRestart(params) #### onResume(params) #### onPreResume(params) #### onPause(params) #### onPrePause(params) #### onStop(params) #### onPreStop(params) #### onDestroy(params) #### onPreDestroy(params) ## UniActivityWindowCallback ### 实例方法 #### onDetachedFromWindow(params) #### onContentChanged(params) #### onWindowAttributesChanged(params, attrs) #### onWindowFocusChanged(params, hasFocus) #### onAttachedToWindow(params) #### onPanelClosed(params, featureId, menu) #### onSearchRequested(params) #### onWindowStartingActionMode(params, callback) #### onWindowStartingActionMode(params, callback, type)_1 #### onActionModeFinished(params, mode) #### onActionModeStarted(params, mode) #### onProvideKeyboardShortcuts(params, data, menu, deviceId) #### onPointerCaptureChanged(params, hasCapture) #### onPreDetachedFromWindow(params) #### onPreContentChanged(params) #### onPreWindowAttributesChanged(params, attrs) #### onPreWindowFocusChanged(params, hasFocus) #### onPreAttachedToWindow(params) #### onPrePanelClosed(params, featureId, menu) #### onPreSearchRequested(params) #### onPreWindowStartingActionMode(params, callback) #### onPreWindowStartingActionMode(params, callback, type)_1 #### onPreActionModeFinished(params, mode) #### onPreActionModeStarted(params, mode) #### onPreProvideKeyboardShortcuts(params, data, menu, deviceId) #### onPrePointerCaptureChanged(params, hasCapture) #### dispatchKeyEvent(params, event) #### dispatchPreKeyEvent(params, event) ## 示例 uvue代码 ```vue ``` uts代码 ```ts let callback : (eventLog : string) => void = (res) => { }; export function onCallbackChange(fn : (eventLog : string) => void) { callback = fn } export class UTSAcvitiyLifeCycleCallback extends UniActivityLifeCycleCallback { constructor() { super() } override onCreate(params : UniActivityParams, savedInstanceState : Bundle | null) { console.log('UTSAcvitiyLifeCycle', 'onCreate', savedInstanceState) callback('onCreate') } override onResume(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onResume', params) callback('onResume') } override onPreResume(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onPreResume', params) callback('onPreResume') } override onStart(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onStart', params) callback('onStart') } override onPreStart(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onPreStart', params) callback('onPreStart') } } export class UTSAcvitiyKeyEventCallback extends UniActivityKeyEventCallback { constructor() { super() } override onKeyDown(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onKeyDown', params, keyCode, '' + event) callback('onKeyDown') } override onPreKeyDown(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onPreKeyDown', params, keyCode, '' + event) callback('onPreKeyDown') } override onKeyLongPress(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onKeyLongPress', params, keyCode, '' + event) callback('onKeyLongPress') } override onPreKeyLongPress(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onPreKeyLongPress', params, keyCode, '' + event) callback('onPreKeyLongPress') } } export class UTSActivityWindowCallback extends UniActivityWindowCallback { constructor() { super() } override dispatchPreKeyEvent(params : UniActivityParams, event : KeyEvent | null) { console.log('UTSActivityWindowCallback', 'dispatchPreKeyEvent', params, '' + event) callback('dispatchPreKeyEvent') } override dispatchKeyEvent(params : UniActivityParams, event : KeyEvent | null) { console.log('UTSActivityWindowCallback', 'dispatchKeyEvent', params, '' + event) callback('dispatchKeyEvent') } override onWindowAttributesChanged(params : UniActivityParams, attrs : WindowManager.LayoutParams) { console.log('UTSActivityWindowCallback', 'onWindowAttributesChanged', '' + attrs) callback('onWindowAttributesChanged') } override onAttachedToWindow(params : UniActivityParams) { console.log('UTSActivityWindowCallback', 'onAttachedToWindow', params) callback('onAttachedToWindow') } override onPanelClosed(params : UniActivityParams, featureId : Int, menu : Menu) { console.log('UTSActivityWindowCallback', 'onPanelClosed', featureId, menu) callback('onPanelClosed') } override onWindowStartingActionMode(params : UniActivityParams, callback : ActionMode.Callback | null) { console.log('UTSActivityWindowCallback', 'onWindowStartingActionMode', callback) callback('onWindowStartingActionMode') } override onProvideKeyboardShortcuts(params : UniActivityParams, data : MutableList | null, menu : Menu | null, deviceId : Int) { console.log('UTSActivityWindowCallback', 'onProvideKeyboardShortcuts', data, menu) callback('onProvideKeyboardShortcuts') } override onPreWindowAttributesChanged(params : UniActivityParams, attrs : WindowManager.LayoutParams) { console.log('UTSActivityWindowCallback', 'onPreWindowAttributesChanged', attrs) callback('onPreWindowAttributesChanged') } override onPrePanelClosed(params : UniActivityParams, featureId : Int, menu : Menu) { console.log('UTSActivityWindowCallback', 'onPrePanelClosed', featureId, menu) callback('onPrePanelClosed') } } export class UTSActivityCallback extends UniActivityCallback { constructor() { super() } override onBackPressed(params : UniActivityParams) { console.log('UTSActivityCallback', 'onBackPressed', params) callback('onBackPressed') } override onPreBackPressed(params : UniActivityParams) { console.log('UTSActivityCallback', 'onPreBackPressed', params) callback('onPreBackPressed') } override onRequestPermissionsResult(params : UniActivityParams, requestCode : Int, permissions : MutableList, grantResults : IntArray) { console.log('UTSActivityCallback', 'onRequestPermissionsResult', params) callback('onRequestPermissionsResult') } } export class UTSActivityComponentCallback extends UniActivityComponentCallback { constructor() { super() } override onConfigurationChanged(params : UniActivityParams, newConfig : Configuration) { console.log('UTSActivityComponentCallback', 'onConfigurationChanged', params, '' + newConfig) callback('onConfigurationChanged') } override onPreConfigurationChanged(params : UniActivityParams, newConfig : Configuration) { console.log('UTSActivityComponentCallback', 'onPreConfigurationChanged', params, '' + newConfig) callback('onPreConfigurationChanged') } } ```