Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
whmsysu committed Sep 5, 2018
1 parent e83e825 commit 79eb56b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
2 changes: 0 additions & 2 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ public void startActivity(Intent intent) {
super.startActivity(intent);
overridePendingTransition(android.R.anim.fade_in,0);
}

@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class DailyFeedPresenter extends BasePresenter<IDailyFeedView> {
private String d_id;
private boolean isLoadMore = false; // 是否加载过更多

private Handler mHandler = new Handler();

public DailyFeedPresenter(Context context) {
this.context = context;
}
Expand Down Expand Up @@ -101,7 +103,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if(has_more.equals("true")) {
isLoadMore = true;
dailyFeedView.setDataRefresh(true);
new Handler().postDelayed(() -> getDailyFeedDetail(d_id,next_pager), 1000);
mHandler.postDelayed(() -> getDailyFeedDetail(d_id,next_pager), 1000);
}
}
}
Expand All @@ -114,4 +116,10 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
});
}

@Override
public void detachView() {
super.detachView();
mHandler.removeCallbacksAndMessages(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DailyFgPresenter extends BasePresenter<IDailyFgView> {
private String has_more;
private String next_pager;
private boolean isLoadMore = false; // 是否加载过更多
private Handler mHandler = new Handler();

public DailyFgPresenter(Context context) {
this.context = context;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
isLoadMore = true;
}
adapter.updateLoadStatus(adapter.LOAD_MORE);
new Handler().postDelayed(() -> getDailyTimeLine(next_pager), 1000);
mHandler.postDelayed(() -> getDailyTimeLine(next_pager), 1000);
}
}
}
Expand All @@ -118,4 +119,10 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
});
}

@Override
public void detachView() {
super.detachView();
mHandler.removeCallbacksAndMessages(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class GankFgPresenter extends BasePresenter<IGankFgView> {
private int page = 1;
private int lastVisibleItem;
private boolean isLoadMore = false; // 是否加载过更多
private Handler mHandler = new Handler();

public GankFgPresenter(Context context) {
this.context = context;
Expand Down Expand Up @@ -104,7 +105,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
.getItemCount()) {
gankFgView.setDataRefresh(true);
isLoadMore = true;
new Handler().postDelayed(() -> getGankData(), 1000);
mHandler.postDelayed(() -> getGankData(), 1000);
}
}
}
Expand Down Expand Up @@ -146,4 +147,10 @@ private String getVideoDesc(Date publishedAt, List<Gank> results) {
}
return videoDesc;
}

@Override
public void detachView() {
super.detachView();
mHandler.removeCallbacksAndMessages(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ZhihuFgPresenter extends BasePresenter<IZhihuFgView> {
private ZhihuListAdapter adapter;
private int lastVisibleItem;
private boolean isLoadMore = false; // 是否加载过更多
private Handler mHandler = new Handler();

public ZhihuFgPresenter(Context context) {
this.context = context;
Expand Down Expand Up @@ -113,7 +114,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
adapter.updateLoadStatus(adapter.LOAD_PULL_TO);
isLoadMore = true;
adapter.updateLoadStatus(adapter.LOAD_MORE);
new Handler().postDelayed(() -> getBeforeNews(time), 1000);
mHandler.postDelayed(() -> getBeforeNews(time), 1000);
}
}
}
Expand All @@ -126,4 +127,9 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
});
}

@Override
public void detachView() {
super.detachView();
mHandler.removeCallbacksAndMessages(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ public class TopStoriesViewPager extends RelativeLayout {

private Context context;
private ViewPager viewPager;
private ViewPagerClickListenner listenner;
private ViewPagerClickListener listenner;
private List<View> dotList;
private int currentItem = 0;// ImageViewpager当前页面的index
private List<ImageView> images;
// 执行周期性或定时任务
private ScheduledExecutorService mScheduledExecutorService;
private Handler handler = new Handler() {
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(currentItem);
}
};

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacksAndMessages(null);
}

public TopStoriesViewPager(Context context) {
super(context);
this.context = context;
Expand Down Expand Up @@ -77,7 +83,7 @@ private void setView() {
}

public void init(List<TopStories> items,TextView tv,
ViewPagerClickListenner clickListenner) {
ViewPagerClickListener clickListenner) {
this.listenner = clickListenner;
images = new ArrayList<>();
dotList = new ArrayList<>();
Expand Down Expand Up @@ -160,7 +166,7 @@ class ViewPagerTask implements Runnable {
public void run() {
if (images != null) {
currentItem = (currentItem + 1) % images.size();
handler.obtainMessage().sendToTarget();
mHandler.obtainMessage().sendToTarget();
}
}
}
Expand Down Expand Up @@ -215,7 +221,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {
}

// 点击事件监听器接口
public interface ViewPagerClickListenner {
public interface ViewPagerClickListener {
/**
* item点击事件监听
*/
Expand Down

0 comments on commit 79eb56b

Please sign in to comment.