> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Game Saves 活动设备变更

> 处理 PlayFab Game Saves 的活动设备变更，防止玩家在多平台之间切换设备时中途丢失游戏进度。

## 处理活动设备变更

当玩家在游戏过程中切换设备时，务必防止他们因同时在多台设备上进行游戏而意外丢失进度。

### 问题场景

1. **玩家在设备 A 上开始游戏**：开始游玩并成为"活动"设备
2. **玩家切换到设备 B**：登录后选择"同步上次保存的数据"
3. **设备 B 变为活动设备**：设备 A 不再是活动设备，但它可能并不知道这一点
4. **进度丢失风险**：玩家可能继续在设备 A 上游玩，从而产生相互冲突的存档状态

### 解决方案：活动设备回调

监听活动设备变更并妥善处理切换过程：

```cpp theme={null}
// Set up the active device changed callback during initialization
hr = PFGameSaveFilesSetActiveDeviceChangedCallback(
    optionalTaskQueue,                    // Use nullptr for immediate callback
    MyActiveDeviceChangedCallback,        // Your callback function
    contextPtr);                          // Optional context for your callback
```

### 回调实现示例

```cpp theme={null}
void MyActiveDeviceChangedCallback(void* context)
{
    // The current device is no longer active for Game Saves
    
    // 1. Pause the game immediately
    
    // 2. Inform the player what happened
    // with a message such as:
    // "Your game progress is being continued on another device."
    // "Returning to main menu to prevent data loss."
    
    // 3. Return to a safe state (main menu)
    
    // 4. Re-initialize Game Saves system when ready to play again
    // (The player will need to sync again from the main menu)
}
```

### XBOX 特定情形

如果你的游戏仅使用 XBOX 的\*\*单点在场（SPOP）\*\*功能登录，则会自动防止上述场景发生。SPOP 确保用户同一时间只能在一台 XBOX 设备上登录。

### 最佳实践

* 面向多平台的游戏**始终应实现此回调**
* 回调触发时**立即暂停游戏**
* 向玩家**清晰说明**为何被返回到菜单
* 通过返回主菜单而非关闭游戏，**便于玩家重新进入**


## Related topics

- [游戏存档快速入门](/zh-CN/services/playfab/player-progression/game-saves/quickstart.md)
- [Game Saves 离线模式](/zh-CN/services/playfab/player-progression/game-saves/offline.md)
- [游戏存档 UI 回调](/zh-CN/services/playfab/player-progression/game-saves/ui-callbacks.md)
- [游戏存档概述](/zh-CN/services/playfab/player-progression/game-saves/overview.md)
- [游戏存档系统对话框](/zh-CN/build/core-features/common/game-save/game-saves-dialogues.md)
