问题
I am trying to make our video app to support Android N multiwindow mode. I have discovered that activity lifecycle becomes confused in multiwindow mode. The phenomenon is when our app layouts on the top screen with the whole screen in portrait, then I click the Home button, the upper app onPause()
called but onStop()
not called.
According to the google guideline https://developer.android.com/guide/topics/ui/multi-window.html#lifecycle, video app should pause video playback in onStop()
callback rather than onPause()
callback.
In this situation, home button is pressed, the activity go background and become not visible to user, our app should pause video playback but we cannot get onStop()
callback. Meanwhile, the activity do not fire onMultiWindowChanged()
callback, this means the activity is still in multiwindow mode though it is in background. The isInMultiWindowMode()
will return true
in this case.
The same issue will occur when the app is in the left screen with the whole screen in landscape.
I have searched for this question and find someone has alreay post issues to google but not handled in the Android Nougat release.
https://code.google.com/p/android/issues/detail?id=215650&can=1&q=multi%20window%20onstop&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened
So, when is the right time to pause our video playback in such situation? If we pause the video in the onPause()
callback, but the activity may be visible to user in multiwindow mode. If we not do, we cannot get the onStop()
callback in this case.
Are there some proper workaround for such cases?
回答1:
When you hit the home button in multi-window mode, the system is in a transient state, allowing the user to select an app to start while your app continues to run (if you're the topmost app, you'll note you can still see the status bar from your app). There is no callback associated with going into this transient mode and you should not change your behavior when entering this transient mode.
Instead, you should continue to play any video - only stop your video when you receive a callback to onStop()
.
回答2:
As per the official document MultiWinodw LifeCycle :
"Multi-window mode does not change the activity lifecycle."
In MultiWindow mode, the only activity which user has interacted most recently will be the top most activity and other activity will go into onPause()
mode as it would be partially visible. When user will try to interact other activity that would go into onResume()
state and it would become topmost activity and rest one would go into onPause()
mode.
Now in case of music player, they have clearly mentioned that you should continue playing your music even if onPause()
method will be called if you are supporting MultiWindow mode.
Only stop your video when you onStop() would be called.
来源:https://stackoverflow.com/questions/39737770/activity-onstop-not-called-when-home-button-is-pressed-in-android-n-multi-wind