当前位置:网站首页>fragment可见性判断
fragment可见性判断
2022-08-05 01:50:00 【TomCat。。。】
1.fragment在activity中之间使用:
这种情况是最简单的,因为此时的fragment的生命周期是和activity的生命周期一致的,当打开另外一个activity的时候,只要监听Fragment的onResume和onPause方法就能够判断其显隐(至于这两个方法什么意思就不多说了,不懂的自己去看activity和fragment的生命周期)
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}2.在ViewPager中的Fragment
ViewPager中Fragment左右切换时,是不会调用onResume()和onPause()方法的。因为这两个方法是跟随activity的生命周期来走的,这个时候我们就可以在fragment中重新 setUserVisibleHint(boolean isVisibleToUser)方法来监听Fragment是否可见
@Override
public voidsetUserVisibleHint(booleanisVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//可见
}else{
//不可见
}
}3.使用show()和hide()方法来显示隐藏fragment
这种情况是在一个activity中fragment切换成另外一个fragment,那么上面两种监听你会发现都没卵用,此时我们可以监听onHiddenChanged(boolean hidden)方法来判断是否对用户可见。
@Override
public void onHiddenChanged(booleanhidden) {
super.onHiddenChanged(hidden);
if(hidden){
//不可见
}else{
//可见
}
}边栏推荐
猜你喜欢

Use of pytorch: Convolutional Neural Network Module
![[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?](/img/eb/535ffaff9b535fbc73a4d56aab0b3a.png)
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?

释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展

自定义线程池

Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?

PCIe Core Configuration

汇编语言之源程序

手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》

从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】

第十四天&postman
随机推荐
【Endnote】Word插入自定义形式的Endnote文献格式
PHP技能评测
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
MySQL学习
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
亚马逊云科技携手中科创达为行业客户构建AIoT平台
2021年11月网络规划设计师上午题知识点(上)
Transfer Learning - Distant Domain Transfer Learning
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
Exercise: Selecting a Structure (1)
Residential water problems
开篇-开启全新的.NET现代应用开发体验
硬实力和软实力,哪个对测试人来说更重要?
Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
oracle将restful接口封装到视图中
[Machine Learning] 21-day Challenge Study Notes (2)
自定义线程池
1349. 参加考试的最大学生数 状态压缩
JZ搜索引擎solr研究-从数据库创建索引
新唐NUC980使用记录:在用户应用中使用GPIO