当前位置: 首页 > news >正文

简单的网站建设方案百度竞价推广代理商

简单的网站建设方案,百度竞价推广代理商,百度免费下载安装,软件开发用什么笔记本问题描述 不保留活动开关打开,把app切入后台,会导致当前展示的Activity被回收,切到前台后重建。 我们有个业务场景是,Activity里面有个ViewPager2,VP里面放Fragment,Fragment的展示需要在Activity中做一些…

问题描述

不保留活动开关打开,把app切入后台,会导致当前展示的Activity被回收,切到前台后重建。
我们有个业务场景是,Activity里面有个ViewPager2,VP里面放Fragment,Fragment的展示需要在Activity中做一些逻辑判断,然后才把ViewPager2的adapter set给ViewPager2,进而Fragment才展示出来(在showContent函数中做的)。在正常情况下,是没有问题的,看log1,A表示Activity,F表示Fragment。

2023-04-24 11:48:14.880 27631-27631/com.sohu.sohuvideo E/lzy: A onCreate
2023-04-24 11:48:14.929 27631-27631/com.sohu.sohuvideo E/lzy: A onStart
2023-04-24 11:48:14.934 27631-27631/com.sohu.sohuvideo E/lzy: A onResume
2023-04-24 11:48:14.970 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:48:14.981 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:48:14.996 27631-27631/com.sohu.sohuvideo E/lzy: F onCreate
2023-04-24 11:48:14.996 27631-27631/com.sohu.sohuvideo E/lzy: F onCreateView
2023-04-24 11:48:14.999 27631-27631/com.sohu.sohuvideo E/lzy: F onViewCreated
2023-04-24 11:48:15.003 27631-27631/com.sohu.sohuvideo E/lzy: F onStart
2023-04-24 11:48:15.004 27631-27631/com.sohu.sohuvideo E/lzy: F onResume()

我们把“不保留活动”开关打开,点击home键切入后台,执行流程如下。
可以看到Activity和Fragment都被回收了

2023-04-24 11:49:37.176 27631-27631/com.sohu.sohuvideo E/lzy: F onStop
2023-04-24 11:49:37.176 27631-27631/com.sohu.sohuvideo E/lzy: A onSaveInstanceState
2023-04-24 11:49:37.181 27631-27631/com.sohu.sohuvideo E/lzy: F onSaveInstanceState
2023-04-24 11:49:37.203 27631-27631/com.sohu.sohuvideo E/lzy: A onDestroy
2023-04-24 11:49:37.210 27631-27631/com.sohu.sohuvideo E/lzy: F onDestroy()

我们再把app切出到前台,Activity和Fragment都会重建,结果app崩溃了

2023-04-23 14:11:35.851 11874-11874/? E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.sohu.sohuvideo, PID: 11874kotlin.UninitializedPropertyAccessException: lateinit property firstPageSessionListForeverLiveData has not been initializedat com.sohu.sohuvideo.chat.fragment.SessionListFragment.subscribeToModel(SessionListFragment.kt:3)at com.sohu.sohuvideo.chat.fragment.SessionListFragment.onViewCreated(SessionListFragment.kt:8)at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:21)at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:25)at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:69)at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:4)at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:75)at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3)at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3)at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:1)at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:5)at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1433)at android.app.Activity.performStart(Activity.java:7976)at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3544)at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:226)at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:206)at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:178)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:102)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2245)at android.os.Handler.dispatchMessage(Handler.java:107)at android.os.Looper.loop(Looper.java:237)at android.app.ActivityThread.main(ActivityThread.java:7840)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:985)

崩溃的原因是,Fragment里有个lateinit属性没有被赋值,看代码逻辑是这个属性是在Activity中创建Fragment的时候传进来的,Fragment是在ViewPager2的Adapter中创建的。说明切到前台,执行流程是正常打开是不一样的,所以才导致这个问题。
切到前台执行流程如下

2023-04-24 11:50:28.542 27631-27631/com.sohu.sohuvideo E/lzy: A onCreate
2023-04-24 11:50:28.547 27631-27631/com.sohu.sohuvideo E/lzy: F onCreate
2023-04-24 11:50:28.576 27631-27631/com.sohu.sohuvideo E/lzy: A onStart
2023-04-24 11:50:28.609 27631-27631/com.sohu.sohuvideo E/lzy: F onCreateView
2023-04-24 11:50:28.610 27631-27631/com.sohu.sohuvideo E/lzy: F onViewCreated
2023-04-24 11:50:28.612 27631-27631/com.sohu.sohuvideo E/lzy: F onStart
2023-04-24 11:50:28.614 27631-27631/com.sohu.sohuvideo E/lzy: A onRestoreInstanceState
2023-04-24 11:50:28.616 27631-27631/com.sohu.sohuvideo E/lzy: A onResume
2023-04-24 11:50:28.621 27631-27631/com.sohu.sohuvideo E/lzy: F onResume()
2023-04-24 11:50:28.656 27631-27631/com.sohu.sohuvideo E/lzy: A showContent
2023-04-24 11:50:30.143 27631-27631/com.sohu.sohuvideo E/lzy: A showContent

我们发现切到前台,Fragment和Activity会重建,生命周期的执行流程如上,这个是因为系统知道这些被回收了,自动重建的。而Fragment中使用firstPageSessionListForeverLiveData是在onViewCreated,所以就报错了。

问题的解决

其实这个问题拔高一点,是activity和Fragment、Fragment和Fragment之前共享数据的问题,我们可以把需要共享的数据,放到依附Activity的ViewModel中,这样只要能获取到这个ViewModel实例就能拿到共享数据,而不是放在Activity中让Activity传给Fragment。

以上面的问题为例,firstPageSessionListForeverLiveData放在ViewModel里面,Fragment被重建的时候,可以直接从Activity的ViewModel中直接拿到这firstPageSessionListForeverLiveData。就可以解决这个问题。

这里需要说明的是,Activity重建后,按照执行流程还是会执行到给ViewPager2设置Adapter的流程中,但是,实际上已经不会再去场景Fragment了,也就是不会再执行Adapter的createFragment函数了。这个不知道为什么,应该是这种销毁重建有特殊处理吧,能把重建的Fragment和ViewPager2能自动关联起来,这块可以以后研究。其实也就是应该这个原因才有的崩溃。
locateTab()是去定位展示哪个Fragment的,是可以正常执行的

    private void showContent(boolean isDataNotEmpty) {Log.e("lzy", "A showContent");PullListMaskController.ListViewState newState = isDataNotEmpty ? PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE : PullListMaskController.ListViewState.EMPTY_BLANK;LogUtils.d(TAG, "fyf--------showContent(): mCurrentState = " + mCurrentState + ", newState = " + newState);if (mCurrentState != newState) {mCurrentState = newState;if (mCurrentState == PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE) {viewBinding.fragmentViewPager.setAdapter(new SessionPagerAdapter(this));viewBinding.slidingTabLayout2.setViewPager(viewBinding.fragmentViewPager, new String[]{"我关注的", "粉丝来信"});viewPagerReady = true;locateTab();showViewStatusWhenResponse(PullListMaskController.ListViewState.LIST_REFRESH_COMPLETE);onFollowUnReadChange();onOtherUnReadChange();} else {showViewStatusWhenResponse(PullListMaskController.ListViewState.EMPTY_BLANK);}}}private class SessionPagerAdapter extends FragmentStateAdapter {public SessionPagerAdapter(@NonNull FragmentActivity fragmentActivity) {super(fragmentActivity);}@NonNull@Overridepublic Fragment createFragment(int position) {SessionListFragment mFragment = new SessionListFragment();Bundle bundle = new Bundle();LogUtils.d(TAG, "fyf--------createFragment(): ");if (position == 0) {bundle.putString(SessionListFragment.TAB_TYPE, SessionListFragment.MY_ATTENTION);followFragment = mFragment;
//                followFragment.setSessionLiveData(followTabLiveData);
//                followFragment.setFirstPageSessionLiveData(followTabFirstPageLiveData);} else {bundle.putString(SessionListFragment.TAB_TYPE, SessionListFragment.OTHER);otherFragment = mFragment;
//                otherFragment.setSessionLiveData(otherTabLiveData);
//                otherFragment.setFirstPageSessionLiveData(otherTabFirstPageLiveData);}mFragment.setArguments(bundle);mFragment.setCallBack(new SessionListFragment.ICallback() {@Overridepublic int getTotalItemCount() {int totalCount = 0;if (followFragment != null) {totalCount += followFragment.getItemCount();}if (otherFragment != null) {totalCount += otherFragment.getItemCount();}return totalCount;}});return mFragment;}

还有一点需要说明的是,ViewPager2是有两个tab,也就是有两个Fragment,如果两个Fragment都创建过,销毁重建的时候,两个Fragment都会重建出来,确定了展示那个Fragment了之后,另一个Fragment又被销毁了,不知道为什么。

2023-04-24 14:54:39.100 7400-7400/? E/lzy: A onCreate
2023-04-24 14:54:39.106 7400-7400/? E/lzy: F onCreate
2023-04-24 14:54:39.106 7400-7400/? E/lzy: F onCreate
2023-04-24 14:54:39.138 7400-7400/? E/lzy: A onStart
2023-04-24 14:54:39.186 7400-7400/? E/lzy: F onCreateView
2023-04-24 14:54:39.187 7400-7400/? E/lzy: F onViewCreated
2023-04-24 14:54:39.190 7400-7400/? E/lzy: F onCreateView
2023-04-24 14:54:39.191 7400-7400/? E/lzy: F onViewCreated
2023-04-24 14:54:39.193 7400-7400/? E/lzy: F onStart
2023-04-24 14:54:39.193 7400-7400/? E/lzy: F onStart
2023-04-24 14:54:39.198 7400-7400/? E/lzy: A onRestoreInstanceState
2023-04-24 14:54:39.199 7400-7400/? E/lzy: A onResume
2023-04-24 14:54:39.203 7400-7400/? E/lzy: F onResume()
2023-04-24 14:54:39.232 7400-7400/? E/lzy: A showContent
2023-04-24 14:54:39.236 7400-7400/? E/lzy: F onPause
2023-04-24 14:54:39.236 7400-7400/? E/lzy: F onResume()
2023-04-24 14:54:39.330 7400-7400/? E/lzy: A showContent
2023-04-24 14:54:49.237 7400-7400/? E/lzy: F onSaveInstanceState
2023-04-24 14:54:49.243 7400-7400/? E/lzy: F onStop
2023-04-24 14:54:49.247 7400-7400/? E/lzy: F onDestroy()

看上面的log,最后两行log,切出前台后,两个Fragment都被重建了,最后有一个被销毁了

参考

Android开发之InstanceState详解
Fragment 的过去、现在和将来


文章转载自:
http://vomiturition.c7512.cn
http://unloved.c7512.cn
http://semivolcanic.c7512.cn
http://covet.c7512.cn
http://ellis.c7512.cn
http://overmaster.c7512.cn
http://dixican.c7512.cn
http://removed.c7512.cn
http://artfully.c7512.cn
http://troponin.c7512.cn
http://cavatina.c7512.cn
http://below.c7512.cn
http://yachter.c7512.cn
http://beguiling.c7512.cn
http://unconspicuous.c7512.cn
http://permanganate.c7512.cn
http://neuk.c7512.cn
http://telemedicine.c7512.cn
http://turkey.c7512.cn
http://griselda.c7512.cn
http://ptyalagogue.c7512.cn
http://update.c7512.cn
http://roose.c7512.cn
http://kootenai.c7512.cn
http://distillate.c7512.cn
http://claustration.c7512.cn
http://psittacism.c7512.cn
http://monoamine.c7512.cn
http://firewood.c7512.cn
http://pappus.c7512.cn
http://fright.c7512.cn
http://basle.c7512.cn
http://handkerchief.c7512.cn
http://arlington.c7512.cn
http://saltationist.c7512.cn
http://divinable.c7512.cn
http://varus.c7512.cn
http://playbill.c7512.cn
http://sogat.c7512.cn
http://smallage.c7512.cn
http://picaninny.c7512.cn
http://anthropophagy.c7512.cn
http://amie.c7512.cn
http://lowlander.c7512.cn
http://phonemics.c7512.cn
http://spy.c7512.cn
http://sully.c7512.cn
http://topkhana.c7512.cn
http://hoverbed.c7512.cn
http://plastic.c7512.cn
http://lipopexia.c7512.cn
http://hypogeusia.c7512.cn
http://senryu.c7512.cn
http://heterology.c7512.cn
http://samphire.c7512.cn
http://gothicism.c7512.cn
http://meagrely.c7512.cn
http://vouge.c7512.cn
http://supervisory.c7512.cn
http://arginase.c7512.cn
http://tco.c7512.cn
http://germless.c7512.cn
http://refrigerator.c7512.cn
http://propitious.c7512.cn
http://ruse.c7512.cn
http://decongestant.c7512.cn
http://fuck.c7512.cn
http://transient.c7512.cn
http://intranet.c7512.cn
http://beggar.c7512.cn
http://thiobacteria.c7512.cn
http://goiterogenic.c7512.cn
http://exhume.c7512.cn
http://aeneous.c7512.cn
http://sarrusophone.c7512.cn
http://unrestful.c7512.cn
http://singularly.c7512.cn
http://kent.c7512.cn
http://blenheim.c7512.cn
http://unsanctified.c7512.cn
http://townwear.c7512.cn
http://echinoderm.c7512.cn
http://prosaism.c7512.cn
http://vedalia.c7512.cn
http://extraordinaire.c7512.cn
http://zora.c7512.cn
http://bruges.c7512.cn
http://crestfallen.c7512.cn
http://manufacturing.c7512.cn
http://scholastic.c7512.cn
http://polystyle.c7512.cn
http://immunodepression.c7512.cn
http://unaspiring.c7512.cn
http://chartist.c7512.cn
http://waygoing.c7512.cn
http://bluegill.c7512.cn
http://housemasterly.c7512.cn
http://lovell.c7512.cn
http://currant.c7512.cn
http://amotivational.c7512.cn
http://www.zhongyajixie.com/news/86099.html

相关文章:

  • 东莞麻涌网站建设今日头条郑州头条新闻
  • 交互效果好的移动端网站百度推广seo
  • 成都网站制作实力乐云seo网站开通
  • 做旅游视频网站搜索引擎关键词优化方案
  • 商城网站合肥网站快速排名提升
  • 钦州市建设局网站北京出大大事了
  • 广州做外贸网站的公司今日大事件新闻
  • 网站是做java还是cseo优化软件哪个好
  • 北京网站建设的服务商宁德市医院
  • 独立站如何推广网站查询平台官网
  • 网站建设有哪些公司兰州seo快速优化报价
  • 中文wordpress网站模板下载失败中国今天最新军事新闻
  • 免费的免抠图素材网站广西seo经理
  • 东莞网站制作网站设计seo网站内部优化
  • 哪里可以找人做网站南京高端品牌网站建设
  • 南京凯盛建设集团官方网站自己的网站怎么推广
  • 做网站的公司挣钱吗磁力蜘蛛
  • 注册公司网站的步骤百度平台官网
  • 网站建设和网络推广外包手机优化大师下载安装
  • 嘉盛集团官方网站营销策划方案内容
  • 网站开发设计心得西安网站建设优化
  • 坂田杨美企业网站建设游戏推广公司
  • 简单h5灰色词优化培训
  • 婚恋网站排名前10网站推广100种方法
  • 建站公司专业定制看b站视频软件下载安装
  • 做调差问卷的网站磁力屋 最好用
  • 好的手机网站推荐泰州seo外包
  • 手机网站设计理念宁波seo网络推广
  • 石家庄公司网站设计线上营销活动有哪些
  • 红色门户网站襄阳网站推广优化技巧