Android 由于系统回收了Activity导致的fragmentUI错乱或数据加载异常等问题的处理。
程序员文章站
2022-06-09 16:08:10
...
Android 由于系统回收了Activity导致的fragmentUI错乱或数据加载异常等问题的处理。
背景:近期厂商反馈的进程长时间在后台,再次进入主页会出现数据加载不出来的情况。
复现办法:
方法1.
进入开发者选项->不保留活动(用户离开后即销毁每个活动)
方法2.
# 找到进程PID
adb shell ps | grep your.app.package
# 结果类似下面这样:
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a111 11111 160 888888 222222 ffffffff 00000000 S your.app.package
# kill掉该进程,如果说没有权限就先执行adb root
adb shell kill -9 11111
# app已经被模拟回收了
当我们打开应用退至后台后会销毁我们的应用,再次进入时输出log如下:
11-13 17:14:12.177 16891-16891/ E/kradio.home: onCreate: this = HorizontalHomePlayerFragment{a6c9877 #0 id=0x7f0902b9 com.***.kradio.k_***.home.HorizontalHomePlayerFragment}
11-13 17:14:12.193 16891-16891/ E/kradio.home: createPresenter:
11-13 17:14:12.754 16891-16891/ E/kradio.home: onCreateView: this = HorizontalHomePlayerFragment{a6c9877 #0 id=0x7f0902b9 com.***.kradio.k_***.home.HorizontalHomePlayerFragment}
11-13 17:14:13.203 16891-16891/ D/kradio.home: HomePlayerFragment onViewCreated
11-13 17:14:13.232 16891-16891/ E/kradio.home: onActivityCreated:
11-13 17:14:13.240 16891-16891/ E/kradio.home: onCreate: this = HorizontalHomePlayerFragment{74a3a8d #2 id=0x7f0902b9 com.***.kradio.k_***.home.HorizontalHomePlayerFragment}
11-13 17:14:13.242 16891-16891/ E/kradio.home: createPresenter:
11-13 17:14:13.245 16891-16891/ D/kradio.home: HomePlayerFragment onDestroyView
11-13 17:14:13.271 16891-16891/ E/kradio.home: onCreateView: this = HorizontalHomePlayerFragment{74a3a8d #2 id=0x7f0902b9 com.***.kradio.k_***.home.HorizontalHomePlayerFragment}
11-13 17:14:13.332 16891-16891/ D/kradio.home: HomePlayerFragment onViewCreated
11-13 17:14:13.334 16891-16891/ E/kradio.home: onActivityCreated:
11-13 17:14:13.345 16891-16891/ E/kradio.home: onResume:
11-13 17:14:13.346 16891-16891/ D/kradio.home: dealPageJump-------->page = -1
11-13 17:14:14.344 16891-16891/ D/kradio.home: onPlayerInitComplete-------->isSuccess = true
11-13 17:14:14.347 16891-16891/ D/kradio.home: requestAudioFocus-------->flag = true
11-13 17:14:14.418 16891-16891/ D/kradio.home: onPlayerInitComplete-------->isSuccess = true
11-13 17:14:14.419 16891-16891/ D/kradio.home: requestAudioFocus-------->flag = true
11-13 17:14:14.462 16891-16891/ E/kradio.home: onDestroy: this = HorizontalHomePlayerFragment{a6c9877 #0 id=0x7f0902b9 com.***.kradio.k_***.home.HorizontalHomePlayerFragment}
通过log我们发现,fragment会被创建两次,且其中有一次走了onDestory。这样就导致了我们的首页数据Socket的连接由于走了onDestroy而被断开了。(Socket在fragment的onDestroy方法中销毁)
解决方法:
相关链接:
https://bbs.csdn.net/topics/391870220?page=1
https://www.jianshu.com/p/f6601220fb6f
我自己使用的方法是在我们的FragmentActivity中添加了对onSaveInstanceState的处理,添加接口对反馈问题的渠道进行处理。
@Override
protected void onSaveInstanceState(Bundle outState) {
//todo 暂时对有问题的渠道进行处理
SaveInstanceStateInter mSaveInstanceStateInter = ClazzUtil.getClazzInstance("com.***.***.flavor.impl.SaveInstanceStateImpl");
if (mSaveInstanceStateInter == null || !mKradioSaveInstanceStateInter.doIgnoreSaveInstanceState(this)) {
Log.i(TAG, "onSaveInstanceState: ");
super.onSaveInstanceState(outState);
}
}
上一篇: ViewPager完美无瑕版