Back button handling
onBackPressed(callback)
Section titled “onBackPressed(callback)”Listen for the user pressing the back key event and optionally intercept it. Only one listener is active at a time; later registrations override earlier ones.
SuperSDK.onBackPressed(() => { if (hasUnsavedData) { showConfirmDialog(); return true; // Return true: intercept and return, the page does not close. } return false; // Return false: do not intercept, execute the default return logic});Callback Return Value Description:
| Return value | Behavior |
|---|---|
true | Intercept the back key, keep the page on the current screen, and do not execute the default back logic. |
false | Do not intercept; execute the default return logic (go back if there is history, otherwise close the page). |
undefined (no return value) | Equivalent to false, do not intercept. |
Use Case Examples:
// Scenario 1: Confirm before leaving the pageSuperSDK.onBackPressed(() => { if (formModified) { const confirmed = confirm('Leave without saving your changes?'); if (!confirmed) { return true; // User chooses to stay, intercept return } } return false; // Allow return});
// Scenario 2: Custom return logic (e.g., closing the popup)SuperSDK.onBackPressed(() => { if (isModalOpen) { closeModal(); return true; // Close the pop-up and intercept the back action. } return false; // No pop-up, normal return});offBackPressed()
Section titled “offBackPressed()”Cancel the back button listener and restore the default back behavior.
SuperSDK.offBackPressed();Important Notes:
- The callback must synchronously return the result; asynchronous operations (async/await) are not supported.
- When the callback execution exceeds the timeout (>300ms), the Native SDK automatically executes the default return logic to prevent freezing.
- Both the Android physical back button and gesture back will trigger.
- On iOS, currently only the navigation bar back button triggers; the swipe-back gesture is not yet supported for interception.