问题:当Activity里有多个fragment时,其中fragmentA根据需求,需要在onResume时需要做请求数据等操作,但是如果直接在fragmentA里的onResume里写东西,当从其他页面返回到fragmentB和fragmentC时,fragmentA里的onResume也会走,这样很浪费流量浪费资源。
问题原因:fragment的生命周期是跟着它所在的activity一起走的,当从其他页面返回到本activity时,本activity会走onResume的生命周期,这个时候本activity里的所有fragment也都会走各个fragment重写的onResume里的代码。
解决方法:
不在fragmentA的onResume里写,而改成下面这样写,不可见时不操作,可见时再操作: