SoFunction
Updated on 2025-03-11

Android realizes the effect similar to IOS right-slide return (cause analysis and solution)

Using the class library SwipeBackLayout

/Issacw0ng/SwipeBackLayout

Problems that arise:

1. When the main activity returns, the screen is black or the return is just seeing the desktop background but not seeing the previous Activity interface.

reason:

Using sliding to return you need to declare android:windowIsTranslucent=true in the Activity's minus theme, and this property sets the Activity to whether the Activity is a transparent theme. When the main Activity adopts a transparent theme, since it is the first in the app Activity stack, you will see the desktop or the black screen when sliding to return.

solve:

The main activity should not turn on the sliding return function (it is not necessary by itself). It is only used in the activities that need to be sliding back. You can define the following themes and set the themes for different activities as needed:

<style name="BaseTheme" parent="@android:style/">
    <item name="android:windowIsTranslucent">true</item>
  </style>
<style name="MainTheme" parent="@android:style/">
    <item name="android:windowIsTranslucent">false</item>
  </style>

2. Another problem occurs during the life cycle of the Activity switch:

When the user opens a new activity or switches to the desktop, the callback is as follows: onPause->onStop.

There is a special case here. If the new Activity adopts a transparent theme, then the current Activity will not callback onStop

Sometimes you need to do some processing in onStop. If you use a transparent theme, it will not be called. An alternative is to execute it in onPause. Of course, it must be noted that no matter whether it is on onStop or onPause, you cannot perform too time-consuming operations, otherwise it will affect the startup of the new activity.

The above is the effect (reason analysis and solution) introduced by the editor to Android to achieve the right scrolling back similar to IOS. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time!