一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
46 行
1.2 KiB
TypeScript
46 行
1.2 KiB
TypeScript
import { BackHandler, Platform } from 'react-native';
|
|
|
|
export type BackButtonHandler = ()=> boolean|Promise<boolean>;
|
|
|
|
export default class BackButtonService {
|
|
private static handlers_: BackButtonHandler[] = [];
|
|
private static defaultHandler_: BackButtonHandler;
|
|
|
|
public static initialize(defaultHandler: BackButtonHandler) {
|
|
this.defaultHandler_ = defaultHandler;
|
|
|
|
// On web, `BackHandler.addEventListener` fails with warning "BackHandler is not supported on web and should not be used."
|
|
if (Platform.OS !== 'web') {
|
|
BackHandler.addEventListener('hardwareBackPress', () => {
|
|
void this.back();
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
|
|
public static async back() {
|
|
if (this.handlers_.length) {
|
|
const r = await this.handlers_[this.handlers_.length - 1]();
|
|
if (r) return r;
|
|
}
|
|
|
|
return await this.defaultHandler_();
|
|
}
|
|
|
|
public static addHandler(handler: BackButtonHandler) {
|
|
for (let i = this.handlers_.length - 1; i >= 0; i--) {
|
|
const h = this.handlers_[i];
|
|
if (h === handler) return false;
|
|
}
|
|
|
|
this.handlers_.push(handler);
|
|
return true;
|
|
}
|
|
|
|
public static removeHandler(handler: BackButtonHandler) {
|
|
this.handlers_ = this.handlers_.filter(h => h !== handler);
|
|
}
|
|
}
|
|
|
|
|