前台Activity旋转,后台Activity被销毁
2017-01-03 22:15 阅读(261)

上周四要发版,结果非常之尴尬,因为我负责的部分,导致到晚上12点仍然无法通过测试验收。 
非常非常尴尬…… 
我负责爱奇艺头条的视频播放部分,一个场景是,首页Feed流A页面支持视频自动播放,点击正在播放的视频,跳转到B页面,B页面使用同一个视频播放器完成续播。

问题复现:在B页面点击全屏按钮,偶现视频停止播放。

追查:点击全屏,播放器内部要调整TextureView大小,这时发现player变成了null, surface也变成了null。置空操作,我是在播放器的release方法里做的。再调试,发现B页面转屏时,A页面的onDestroy被调用,我在A页面的onDestroy方法里做了销毁播放器的操作。

what the hell? 
为什么B页面转全屏,后台的A页面被销毁! 
怀疑是资源回收导致的,但是出现问题的几次查看资源消耗情况,内存CPU占用都不高。

再调试发现,A页面在onDestroy被调用后,onCreate紧接着被调用,这个符合转屏时Activity的销毁重建路径。

But,what the hell? 我是对B页面转屏,为什么A页面也被调用了转屏?

/**
     * Change the desired orientation of this activity.  If the activity
     * is currently in the foreground or otherwise impacting the screen
     * orientation, the screen will immediately be changed (possibly causing
     * the activity to be restarted). Otherwise, this will be used the next
     * time the activity is visible.
     *
     * @param requestedOrientation An orientation constant as used in
     * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}.
     */
    public void setRequestedOrientation(@ActivityInfo.ScreenOrientation int requestedOrientation)

注释里强调了foreground, 想起了一件事,B页面支持滑动关闭,而滑动关闭是在滑动时把Activity变成透明属性,是不是这个影响了转屏操作?

@Override
            public void onEdgeTouch(int edgeFlag) {
                InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive() && mActivity.getCurrentFocus()!=null) {
                    imm.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), 0);
                }
                Utils.convertActivityToTranslucent(mActivity);
            }

果然如此…… 
在B页面触发了onEdgeTouch, 导致Activity变成透明的。再全屏操作时,导致后面的A页面也执行了转屏操作。


作者:kyleada_dl

返回顶部