valcredentialManager=CredentialManager.create(context)// On a successful authentication create a Restore Key// Pass in the context and CreateRestoreCredentialRequest objectvalresponse=credentialManager.createCredential(context,createRestoreRequest)
// Fetch the Authentication JSON from servervalauthenticationJson=fetchAuthenticationJson()// Create the GetRestoreCredentialRequest objectvaloptions=GetRestoreCredentialOption(authenticationJson)valgetRequest=GetCredentialRequest(listOf(options))// The restore key can be fetched in two scenarios to// 1. On the first launch of app on the device, fetch the Restore Key// 2. In the onRestore callback (if the app implements the Backup Agent)valresponse=credentialManager.getCredential(context,getRequest)
// Create a ClearCredentialStateRequest objectvalclearRequest=ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)// On user log-out, clear the restore keyvalresponse=credentialManager.clearCredentialState(clearRequest)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["# Restore app credentials when setting up a new device\n\nCredential Manager's *Restore Credentials* feature allows users to restore their\napp accounts when setting up a new device. This API is in developer preview and\navailable on all devices that have Android 9 or higher and Google Play services\n(GMS) Core version 242200000 or higher. The benefits of the Restore Credentials\nfeature includes:\n\n- **Seamless user experience**: Users can restore their app account without needing to manually sign in to each individual app.\n- **Increased user engagement**: Users are more likely to continue using your app if they can restore their account when setting up a new device.\n- **Reduced development effort**: the Restore Credentials feature is integrated with Credential Manager, so developers who already support passkeys can add credential restoration capabilities.\n\nHow it works\n------------\n\nYou can use Restore Credentials to create, get, and clear the relevant\ncredentials.\n\n1. **Create the Restore Credential**: When the user signs in to your app, create a Restore Credential associated with their account. This credential is stored locally and synced to the cloud if the user has enabled Google Backup and end to end encryption is available (apps can opt out of syncing to the cloud)\n2. **Get the Restore Credential**: When the user sets up a new device, your app can request the Restore Credential from Credential Manager. This lets you sign the user in automatically without requiring any additional input.\n3. **Clear the Restore Credential**: When the user signs out of your app, you should delete the associated Restore Credential.\n\nThe Restore Credentials feature can integrate smoothly with backend systems that\nhave already implemented passkeys. This compatibility stems from the fact that\nboth passkeys and restore keys (credential type used by the Restore Credentials\nfeature) adhere to the same underlying technical specifications. This alignment\nmakes sure that the Restore Credentials process can effectively retrieve and\nreinstate user credentials stored in passkey-enabled systems, providing a\nconsistent and user-friendly experience across different platforms and\nauthentication methods.\n**Figure 1.** Diagram that depicts restoring an app data to a new device using a restore credential, including creating the credential, initiating a restore flow, and automatic user sign-in **Note:** Restore keys are the sole credential type supported for this feature. Other credential types, such as passwords, security tokens, or biometric data, are not compatible.\n\nImplementation\n--------------\n\nThe Restore Credentials API is available through the Credential Manager Jetpack\nlibrary. To get started, follow these steps:\n\n1. Add the Credential Manager dependency to your project.\n\n // build.gradle.kts\n implementation(\"androidx.credentials:credentials:1.5.0-alpha03\")\n\n2. Create a `CreateRestoreCredentialRequest` object.\n\n | **Note:** Set the **`isCloudBackupEnabled`** flag as **`true`** to enable the auto backup of the `restoreKey` (if the user has Google Backup enabled and end to end encryption is available). Set it to **`false`** if you want the `restoreKey` to be stored locally and not in the cloud.\n3. Call the `createCredential()` method on the `CredentialManager` object.\n\n val credentialManager = CredentialManager.create(context)\n\n // On a successful authentication create a Restore Key\n // Pass in the context and CreateRestoreCredentialRequest object\n val response = credentialManager.createCredential(context, createRestoreRequest)\n\n This generated *restore credential* is a type of webauthn credential, and is\n known as a *restore key*.\n4. When the user sets up a new device, call the `getCredential()` method on the\n `CredentialManager` object.\n\n // Fetch the Authentication JSON from server\n val authenticationJson = fetchAuthenticationJson()\n\n // Create the GetRestoreCredentialRequest object\n val options = GetRestoreCredentialOption(authenticationJson)\n val getRequest = GetCredentialRequest(listOf(options))\n\n // The restore key can be fetched in two scenarios to\n // 1. On the first launch of app on the device, fetch the Restore Key\n // 2. In the onRestore callback (if the app implements the Backup Agent)\n val response = credentialManager.getCredential(context, getRequest)\n\n5. When the user signs out of your app, call the `clearCredentialState()`\n method on the `CredentialManager` object.\n\n // Create a ClearCredentialStateRequest object\n val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)\n\n // On user log-out, clear the restore key\n val response = credentialManager.clearCredentialState(clearRequest)\n\nIf you're using a backup agent, perform the `getCredential` part within the\n`onRestore` callback. This makes sure that the app's credentials are restored\nimmediately after the app data is restored.\n| **Note:** Restore Credentials does not handle the scenario where an app is reinstalled on the same device. Uninstalling an app is interpreted as an intent to delete the corresponding restore key from that device.\n\nFrequently asked questions\n--------------------------\n\n**Q1. What is the difference between a restore key and a passkey?**\n\nA restore key functions similarly to a passkey but is specifically designed for\naccount restoration on new devices. When you use a password manager such as\nGoogle Password Manager to authenticate, your usable passkeys and passwords are\ndisplayed, while the restore key is not, as it's not intended for regular\nsign-ins.\n\n**Q2. Is a restore key a one-time use credential?**\n\nNo, a restore key isn't a one-time use credential. Credential Manager is\nstateless and unaware of user activity, so it cannot automatically delete the\nkey after use.\n\nRestore keys are only removed in the following situations:\n\n- **System-level actions**: Users uninstall the app or clear its data.\n- **App-level calls** : You programmatically delete the key by calling [`CredentialManager#clearCredentialState()`](/reference/androidx/credentials/CredentialManager#clearCredentialState(androidx.credentials.ClearCredentialStateRequest)) when you handle user log out in your app's code.\n\n**Q3. Does the Restore Credentials feature only work for a new device?**\n\nYes, this feature is designed for the initial setup of a new Android-powered\ndevice, as it is tied directly to the system's Backup and Restore functionality.\n\n**Q4. Can I use Restore Credentials to silently sign in a user to any device\nthat has the same Google Account logged in?**\n\nNo, the Restore Credentials feature is not intended to be used for general\nsign-in across devices. It only works in one specific scenario: when a user is\nsetting up a new device by restoring a backup from their old device. Both\ndevices must be linked to the same Google Account for this one-time restore\nprocess to succeed.\n\n**Q5. My organization has one main app and multiple sub-apps. Can one restore\nkey work for all of these apps?**\n\nNo. A restore key is tied to an application's unique package name. Since your\nmain app and each sub-app have different package names, you need to create a\nseparate restore key for every app.\n\n**Q6. Is a passkey required to create a restore key for a user's account?**\n\nNo, a passkey is not required. The ability to create a Restore Key is\nindependent of the user's sign-in method. Its purpose is to save the user's\ncurrent authenticated state. As long as the user is actively signed in to your\napp, you can generate a Restore Key for them.\n\n**Q7. Can the user delete the restore key?**\n\nNo, the user has no direct control over the restore key. The app logic is\nresponsible for managing restore keys.\n\nFor security, we recommend that the app automatically deletes the key whenever a\nuser signs out. This makes sure that the next time they open the app on that\nsame device, they are properly signed out and will be prompted to sign in again.\n\n**Q8. Can I use Restore Credentials without setting allowBackup to true in my\nmanifest?**\n\nYes, the Restore Credentials feature works regardless of whether allowBackup is\nset to true or not.\n\n**Q9. How would Restore Credentials work for users who have multiple logged-in\naccounts on the same app?**\n\nThe Restore Credentials feature is designed to work with only one account at a\ntime."]]