Overview
You will learn
- How to handle application restore and passcode timeouts using the Flows component
- How to reset an application using the Flows component
- How to logout of an application using the Flows component
Prerequisites
- You have Set Up a BTP Account for Tutorials. Follow the instructions to get an account, and then to set up entitlements and service instances for the following BTP services.
- SAP Mobile Services
- You completed Try Out the SAP BTP SDK Wizard for Android.
- You completed Get Familiar with the Flows Component by a Wizard Generated Application.
- You completed Customize the Onboarding Flow.
Steps
Details
You will learn
- How to handle application restore and passcode timeouts using the Flows component
- How to reset an application using the Flows component
- How to logout of an application using the Flows component
After onboarding, the next time users open the mobile app, the restore flow will be started. Basically, the restore flow consists of the screen unlocking the app with either the passcode or biometric information, if enabled. The restore flow will also check whether the passcode is expired based on the setting defined in the passcode policy. If the passcode is expired, the restore flow will ask the user to create a new passcode and launch a “change passcode” flow.
Similar to the onboarding flow, the restore flow will also listen to the flow states and notify corresponding events to the client code.
The flows component will automatically determine whether to use the onboarding or restore flow, so the app can use the same client code for both flows.
Open the project you previously created using the SAP BTP SDK Wizard for Android.
In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeWelcomeActivityto openWelcomeActivity.kt.On Windows, press
Ctrl+F12, or, on a Mac, presscommand+F12, and typestartOnboardingto move to thestartOnboardingmethod. As we mentioned in Get Familiar with the Flows Component by a Wizard Generated Application, this method starts an onboarding flow. After onboarding, the next time the app starts, the same method is called and the Flows component detects that the user is already onboarded and starts the restore flow. The client code does not need handle the logic to explicitly start a restore flow.!

Flow restore starting method The restore flow will notify the same events as the onboarding flow and one additional
UnlockWithPasscodeevent, which is specific to the restore flow. When the app is unlocked using a passcode, the client code can get the passcode from theonUnlockWithPasscodecallback of theFlowStateListenerinstance and open the secure store. Customize the Onboarding Flow explains the events notified in the onboarding flow.When the app is put to background, the Flows component will monitor whether the passcode is timed out based on the “Lock Timeout” value defined in the passcode policy. When the passcode is timed out and the app is put to foreground again, a timeout unlock flow will be started. This is the
FlowType.TIMEOUT_UNLOCKflow type, which is used internally by the Flows component for the passcode timeout scenario. This flow’s function is exactly the same as that of the restore flow.
Open the project you previously created using the SAP BTP SDK Wizard for Android.
In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeWelcomeActivityto openWelcomeActivity.java.On Windows, press
Ctrl+F12, or, on a Mac, presscommand+F12, and typestartFlowto move to thestartFlowmethod. As we mentioned in Get Familiar with the Flows Component by a Wizard Generated Application, this method starts an onboarding flow. After onboarding, the next time the app starts, the same method is called and the Flows component detects that the user is already onboarded and starts the restore flow. The client code does not need handle the logic to explicitly start a restore flow.!

Flow restore starting method The restore flow will notify the same events as the onboarding flow and one additional
UnlockWithPasscodeevent, which is specific to the restore flow. When the app is unlocked using a passcode, the client code can get the passcode from theonUnlockWithPasscodecallback of theFlowStateListenerinstance and open the secure store. Customize the Onboarding Flow explains the events notified in the onboarding flow.When the app is put to background, the Flows component will monitor whether the passcode is timed out based on the “Lock Timeout” value defined in the passcode policy. When the passcode is timed out and the app is put to foreground again, a timeout unlock flow will be started. This is the
FlowType.TIMEOUT_UNLOCKflow type, which is used internally by the Flows component for the passcode timeout scenario. This flow’s function is exactly the same as that of the restore flow.
There may be occasions when the user wants to reset the app to initial state. The reset flow is designed to clear all the application data, user data, and security data managed by the Flows component.
Open the project you previously created using the SAP BTP SDK Wizard for Android.
In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeSettingsFragmentto openSettingsFragment.kt.On Windows, press
Ctrl+F12, or, on a Mac, presscommand+F12, and typestartResetFlowto move to thestartResetFlowmethod. To start the flow to reset application, set the flow type toFlowType.RESETfor theFlowContextinstance, and then start the flow with thisFlowContextinstance. The entire process to reset the application will be handled automatically.!

App reset flow When the reset flow is started, the default behavior is for a dialog to be displayed, asking the user for confirmation.
!

App reset dialog You can customize your client code so that the reset flow hides this dialog by setting the value of
needConfirmWhenResetparameter tofalsefor theFlowOptionsinstance and set thisFlowOptionsinstance for theFlowContextinstance to start the reset flow.Kotlinval flowContext = FlowContextBuilder() .setFlowType(FlowType.RESET) .setFlowOptions(FlowOptions( needConfirmWhenReset = false )) .build() Flow.start(this, flowContext)Before removing all the data managed by the Flows component, the reset flow will notify the
ApplicationResetevent. You can use theonApplicationResetcallback of theFlowStateListenerinstance to insert its own logic for application reset, for example to clear the data managed by the client code and un-register the push token.In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeWizardFlowStateListenerto openWizardFlowStateListener.kt. On Windows, pressCtrl+F12, or, on a Mac, presscommand+F12, and typeonApplicationResetto move to theonApplicationResetmethod.!

App reset listener
Open the project you previously created using the SAP BTP SDK Wizard for Android.
In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeSettingsFragmentto openSettingsFragment.java.On Windows, press
Ctrl+F12, or, on a Mac, presscommand+F12, and typeonCreatePreferencesto move to theonCreatePreferencesmethod. To start the flow to reset the application, set the flow type toFlowType.RESETfor theFlowContextinstance, and then start the flow with thisFlowContextinstance. The entire process to reset application will be handled automatically.!

App reset flow When the reset flow is started, the default behavior, is for a dialog to be displayed, asking the user for confirmation.
!

App reset dialog You can customize your client code so that the reset flow hides this dialog by overriding the
isResetConfirmationNeededfunction of theFlowOptionsinstance to returnfalseand set thisFlowOptionsinstance for theFlowContextinstance to start the reset flow.JavaFlowContext flowContext = new FlowContextBuilder() .setFlowType(FlowType.RESET) .setFlowOptions(new FlowOptions(){ @Override public boolean isResetConfirmationNeeded() { return false; } }).build(); Flow.start(this, flowContext);Before removing all the data managed by the Flows component, the reset flow will notify the
ApplicationResetevent. You can use theonApplicationResetcallback of theFlowStateListenerinstance to insert its own logic for application reset, for example to clear the data managed by the client code and un-register the push token.In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeWizardFlowStateListenerto openWizardFlowStateListener.java. On Windows, pressCtrl+F12, or, on a Mac, presscommand+F12, and typeonApplicationResetto move to theonApplicationResetmethod.!

App reset listener
The logout flow will try to logout the current user if network is available, then remove the OAuth2 token of the current user if the app is authenticated with OAuth2. No matter the network is available or not, after the flow, the user needs to be authenticated again when making an API call to the server.
Open the project you previously created using the SAP BTP SDK Wizard for Android.
In Android Studio, on Windows, press
Ctrl+N, or, on a Mac, presscommand+O, and typeEntitySetListActivityto openEntitySetListActivity.kt.On Windows, press
Ctrl+F12, or, on a Mac, presscommand+F12, and typeonOptionsItemSelectedto move to theonOptionsItemSelectedmethod. To start the flow to logout of the application, set the flow type toFlowType.LOGOUTfor theFlowContextinstance, and then start the flow with thisFlowContextinstance.!

Flow logout starting method When the logout flow is started, the default behavior is for a dialog to be displayed, asking the user for confirmation.
!

App logout dialog You can customize your client code so that the logout flow hides this dialog by setting the value of
needConfirmWhenLogoutparameter tofalsefor theFlowOptionsinstance and set thisFlowOptionsinstance for theFlowContextinstance to start the reset flow.Kotlinval flowContext = FlowContextBuilder() .setFlowType(FlowType.LOGOUT) .setFlowOptions(FlowOptions( needConfirmWhenLogout = false )) .build() Flow.start(this, flowContext)Logout flow will not automatically remove the push registration. Client code can implement the callback function
FlowActivityResultCallbackto delete the Firebase push token if it does not want to receive push notifications after logout. The logout flow will trigger the callback function after a successful logout.KotlinFlow.start( this, FlowContextRegistry.flowContext. copy( flowType = FlowType.LOGOUT)){ _, resultCode, _-> if(resultCode ==RESULT_OK) { SDKInitializer.getService (FirebasePushService::class)?.also { FirebaseMessaging.getInstance(). deleteToken().addOnSuccessListener { Log.e("PushService","PushService token was revoked!") } .addOnFailureListener { e1: Exception? -> Log.e("PushService","PushService token couldn't be revoked!:$e1") } } } }
Congratulations! You now have learned how to restore, reset and logout an application using the Flows component!
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.