Development (App Continuity)

To have a seamless experience when using a gallery app, the photo that is displayed after folding or unfolding the device should be the same as the previous. You need to store the position or ID of the photo as Android destroys and recreate the activity when folding and unfolding the phone. Use Bundle with its key/value pair to store and restore the values. In addition, you need to consider the screen rotation in order to keep the app running, as Android restarts the activity of the app after the configuration changes.

Step 1: Handle the configuration change

To prevent the activity from restarting, handle the configuration change for MainActivity in AndroidManifest.xml file.


Step 2: Save the instance state by storing the data

Before the activity is destroyed when the screen changes from main to cover display or vice versa, Android provides us an override method called onSaveInstanceState. It accepts a Bundle as a parameter. Use putInt methods to store data into key/value pair.

override fun onSaveInstanceState(outState: Bundle) {
      outState.putInt("Currentposition", currentImagePosition)
      Log.i("TAG", "onSave "+currentImagePosition)

Step 3: Retrieve the stored values

When the app is first developed, the savedInstanceState will be null inside the onCreate method. If it is not null, you can retrieve the stored values from the Bundle using getInt and the key name you set up in Step 2 while storing.

if(savedInstanceState != null) {
  currentImagePosition = savedInstanceState.getInt("Currentposition")
  Log.i("TAG", "onSaved " + savedInstanceState.getInt("Currentposition"))

Step 4: Run the app

After building the APK, you can run the gallery app on a Galaxy Fold or Galaxy Z Fold2 and see how the app works continuously even when you fold and unfold the device.If you don’t have any physical device, you can also test it on a Remote Test Lab device.

If you wish to learn how to optimize your app on the large screen of foldable devices, you may continue with the next topic.