1. Background

After opening the floating window, the small window will float in the APP and the desktop, and keep all the state of the floating window page

preview

video

Routing is introduced

image

Feature overview (⚠️: [] Marks with pits, explanations and solutions behind)

  1. Splash -> Home page -> Details page -> Floating Window Page -> Return to desktop -> Click desktop App icon -> [Floating Window Page]
  2. Splash -> Home -> Details -> Float Window -> Open float window -> Float window -> Float window
  3. Splash -> Home -> Details -> Floating Window -> Open floating Window -> Return to desktop -> Click Floating Window -> Floating Window -> [Return to Details]

2. Functional realization

  • Android :launchMode=”singleInstance”
  • Add permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Copy the code
  • Check hover window permissions
  • Click Open to put the floating window in the background and add a small window
moveTaskToBack(true)

var windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.addView(...)
Copy the code
  • Click the small window to bring the float page to the foreground
var intent = Intent(it, FloatWindowActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
it.startActivity(intent)
Copy the code
  • Go to the floating window page. Close the small window and keep the floating window page
override fun onRestart(a) {
    super.onRestart()
    closeFloatWindow(this, exitFloatActivity = false)}Copy the code
  • Click close the small window and finish the hover window in the background
closeFloatWindow(this, exitFloatActivity = true)
Copy the code

Step pit level solutions

  1. In the first function preview, click the desktop App icon to display the details page and expect to display the hover page

Explanation:

By default, the float page of routing stack A is opened, not the float page of routing stack B, because the float page is set to singleInstance.

Solution:

Listen for the app to come back to the foreground in the hover window page, and if not in the small window, pull the routing stack back to the foreground

if (FloatWindowHelper.instance.dragFloatWrapper == null) {
    var intent = Intent(this, FloatWindowActivity::class.java)
        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
    startActivity(intent)
}
Copy the code

2. The third item in the function preview: click “back” on the floating window page to return to the desktop, expecting to return to the details page

Explanation:

In the previous step, click the floating window, and the route jumps to the floating window page. At this time, only routing stack B is enabled, so the return is directly back to the desktop

Solution:

If the app(default routing stack A) is started first when clicking the hover window on the desktop, there is A small detail here: the page of LAUNCHER set in our manifest file will be started, namely the Splash page. Here, we just need to finish it and do not need to automatically jump to the home page

If (isAppInBg) {context.let {val intent = It. PackageManager. GetLaunchIntentForPackage (it. PackageName) it. StartActivity (intent) / / tell splash don't need to jump to the home page, and then back to jump needJumpToMain = false timer.schedule(object : TimerTask() { override fun run() {[link](https://note.youdao.com/) needJumpToMain = true } }, 1200) } isAppInBg = false }Copy the code

The source code has been uploaded to Github