使用HMRouter跳转其他组件再返回,当前组件Radio未保持选中状态,问题代码如下:
ohpm install @hadss/hmrouter
{
"dependencies": {
"@hadss/hmrouter-plugin": "^1.2.0" // 使用npm仓版本号
},
} // 工程根目录/hvigorfile.ts
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { appPlugin } from '@hadss/hmrouter-plugin';
export default {
system: appTasks,
plugins: [appPlugin({ ignoreModuleNames: [ /** 不需要扫描的模块 **/ ] })]
}; export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
// 日志开启需在init之前调用,否则会丢失初始化日志
HMRouterMgr.openLog('INFO')
HMRouterMgr.init({
context: this.context
})
}
} // Index.ets
import { HMDefaultGlobalAnimator, HMNavigation, HMRouterMgr } from '@hadss/hmrouter';
import { AttributeUpdater } from '@kit.ArkUI';
@Entry
@Component
struct Index {
modifier: NavModifier = new NavModifier();
build() {
Column() {
HMNavigation({
navigationId: 'mainNavigation', options: {
standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
modifier: this.modifier
}
}) {
Column({ space: 20 }) {
Row() {
Radio({ value: '1', group: 'radioGroup' })
.checked(true)
.radioStyle({
checkedBackgroundColor: '#OD5AF5'
})
.height(20)
.width('5%')
}
Row() {
Radio({ value: '1', group: 'radioGroup' })
.checked(false)
.radioStyle({
checkedBackgroundColor: '#OD5AF5'
})
.height(20)
.width('5%')
}
Button('click')
.onClick(() => {
HMRouterMgr.push({
navigationId: 'mainNavigation',
pageUrl: 'TwoPage'
})
})
}.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
}
}
.height('100%')
.width('100%')
}
}
class NavModifier extends AttributeUpdater<NavigationAttribute> {
initializeModifier(instance: NavigationAttribute): void {
instance.mode(NavigationMode.Stack);
instance.navBarWidth('100%');
}
} // TwoPage.ets
import { HMRouter, HMRouterMgr } from '@hadss/hmrouter'
@HMRouter({ pageUrl: 'TwoPage' })
@Component
export struct TwoPage {
@State isShow: boolean = true
build() {
Column({ space: 20 }) {
Row() {
Radio({ value: '2', group: 'radioGroup' })
.checked(this.isShow)
.radioStyle({
checkedBackgroundColor: Color.Green
})
.height(20)
.width(20)
.margin({ bottom: '10px' })
.onChange((isChecked: boolean) => {
if (isChecked) {
}
})
}.margin(10);
Button('Back')
.width('80%')
.onClick(() => {
this.isShow = false
HMRouterMgr.pop({
navigationId: 'mainNavigation',
})
})
}.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
} 问题效果预览:

修改两个组件中的group为不同值即可解决问题。完整代码如下:
import { HMDefaultGlobalAnimator, HMNavigation, HMRouterMgr } from '@hadss/hmrouter';
import { AttributeUpdater } from '@kit.ArkUI';
@Entry
@Component
struct Index {
modifier: NavModifier = new NavModifier();
build() {
Column() {
HMNavigation({
navigationId: 'mainNavigation', options: {
standardAnimator: HMDefaultGlobalAnimator.STANDARD_ANIMATOR,
dialogAnimator: HMDefaultGlobalAnimator.DIALOG_ANIMATOR,
modifier: this.modifier
}
}) {
Column({ space: 20 }) {
Row() {
Radio({ value: '1', group: 'radioGroup' })
.checked(true)
.radioStyle({
checkedBackgroundColor: '#0D5AF5'
})
.height(20)
.width('5%');
};
Row() {
Radio({ value: '1', group: 'radioGroup' })
.checked(false)
.radioStyle({
checkedBackgroundColor: '#0D5AF5'
})
.height(20)
.width('5%');
};
Button('click')
.onClick(() => {
HMRouterMgr.push({
navigationId: 'mainNavigation',
pageUrl: 'TwoPage'
});
});
}.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%');
};
}
.height('100%')
.width('100%');
}
}
class NavModifier extends AttributeUpdater<NavigationAttribute> {
initializeModifier(instance: NavigationAttribute): void {
instance.mode(NavigationMode.Stack);
instance.navBarWidth('100%');
}
} import { HMRouter, HMRouterMgr } from '@hadss/hmrouter';
@HMRouter({ pageUrl: 'TwoPage' })
@Component
export struct TwoPage {
@State isShow: boolean = true;
build() {
Column({ space: 20 }) {
Row() {
Radio({ value: '2', group: 'radioGroup1' })
.checked(this.isShow)
.radioStyle({
checkedBackgroundColor: Color.Green
})
.height(20)
.width(20)
.margin({ bottom: '10px' })
.onChange((isChecked: boolean) => {
if (isChecked) {
}
});
}.margin(10);
Button('Back')
.width('80%')
.onClick(() => {
this.isShow = false;
HMRouterMgr.pop({
navigationId: 'mainNavigation',
});
});
}.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
}