ionic-team / ionic-team/ionic-framework

bug: Angular Ionic Android back button callback is executing twice

未关闭
#30,115 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
triage
主要语言
TypeScript
星标
52.7k
派生
13.3k
平均合并
1 天 15 小时
30 天内合并 PR
51

描述

### Prerequisites

- [X] I have read the [Contributing Guidelines](https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#creating-an-issue).
- [X] I agree to follow the [Code of Conduct](https://ionicframework.com/code-of-conduct).
- [X] I have searched for [existing issues](https://github.com/ionic-team/ionic-framework/issues) that already report this problem, without success.

### Ionic Framework Version

v8.x

### Current Behavior

`app.component.html`:
```html



```

my page (component) TS file:
```ts
import { Component, inject, OnDestroy, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { Subscription } from 'rxjs';
import { Platform } from '@ionic/angular/standalone';

@Component({
selector: 'e-test',
standalone: true,
imports: [CommonModule, RouterModule],
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss'],
})
export class TestComponent implements OnInit, OnDestroy {
private readonly _platform = inject(Platform);
private _backSub!: Subscription;

ngOnInit() {
this._backSub = this._platform.backButton.subscribeWithPriority(
101,
() => {
console.log('back button called');
},
);
}

ngOnDestroy(): void {
if (this._backSub) this._backSub.unsubscribe();
}
}
```

As far as I can see, everything is correct! But my callback will be called twice!
Can someone help? Thanks a lot ❤️

### Expected Behavior

The callback should be called only once, because we subscribed to `subscribeWithPriority` only once... And we also unsubscribe from it, whenever the component is destroyed.

### Steps to Reproduce

1. Simply click the Android native back button.
2. Then you will see that 'back button called' console log message shows twice.

### Code Reproduction URL

https://stackblitz.com/edit/wusmwpve?file=src%2Fapp%2Fexample.component.ts

### Ionic Info

Ionic:

Ionic CLI : 7.2.0

Utility:

cordova-res : not installed globally
native-run : not installed globally

System:

NodeJS : v20.11.0
npm : 10.2.4
OS : Windows 10

### Additional Information

_No response_

贡献指南

打开贡献指南

调研方向

Start by running the linked StackBlitz reproduction and inspect app.component.html and the example component TypeScript file, especially the back-button subscription and its cleanup. Confirm whether one Android back-button press produces two logs, then trace the relevant framework behavior to identify the cause; done means the callback runs once and the reproduction still works.

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, typescript
领域
frontend, mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。