Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
chejdj committed May 30, 2019
1 parent b2ad440 commit 6fdd2bc
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
### 版本更新说明

```
v2.2.0 Fragment的懒加载和内存泄漏问题
v2.1.0 引入滴滴的哆啦A梦,修改一些Bugly的上Bug
v2.0.0 加入微信分享+修改了部分Bug
v1.4.2 更新部分界面,以及无网络及网络差的友好显示,及Bugly上面的Bug
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.chejdj.wanandroid"
minSdkVersion 21
targetSdkVersion 28
versionCode 11
versionName "2.1.0"
versionCode 12
versionName "2.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk{
abiFilters 'armeabi-v7a','arm64-v8a'
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":11,"versionName":"2.1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":12,"versionName":"2.2.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.chejdj.wanandroid.R;
import com.chejdj.wanandroid.network.bean.article.Article;
Expand All @@ -27,6 +29,8 @@ public class CommonArticleListFragment extends ViewPaperLazyLoadFragment impleme
RecyclerView recyclerView;
@BindView(R.id.swipe_refresh)
SwipeRefreshLayout swipeRefreshLayout;
@BindView(R.id.noData)
LinearLayout noDataLayout;
private int currentPage;
private int totalPage;
private int type;
Expand Down Expand Up @@ -127,8 +131,22 @@ public void updateArticles(ArticleData data) {
if (totalPage == 0) {
totalPage = data.getPageCount();
}
currentPage = data.getCurPage();
adapter.addData(data.getListData());
adapter.loadMoreComplete();
if (data.getListData().isEmpty() && noDataLayout.getVisibility() == View.GONE) {
noDataLayout.setVisibility(View.VISIBLE);
} else if (noDataLayout.getVisibility() == View.GONE) {
if (noDataLayout.getVisibility() == View.VISIBLE) {
noDataLayout.setVisibility(View.GONE);
}
currentPage = data.getCurPage();
adapter.addData(data.getListData());
adapter.loadMoreComplete();
}
}

@Override
public void loadDataFail() {
if (noDataLayout.getVisibility() == View.GONE) {
noDataLayout.setVisibility(View.VISIBLE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
import java.util.List;

public class CommonPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragmentList;
private List<String> subTitles;
private List<Integer> cidNumbers;
private int type;

public CommonPagerFragmentAdapter(List<String> subTitles, List<Fragment> fragments, FragmentManager fm) {
public CommonPagerFragmentAdapter(List<String> subTitles, List<Integer> cidNumbers, int type, FragmentManager fm) {
super(fm);
this.subTitles = subTitles;
this.fragmentList = fragments;

this.cidNumbers = cidNumbers;
this.type = type;
}

@Override
public Fragment getItem(int i) {
return fragmentList == null ? null : fragmentList.get(i);
return CommonArticleListFragment.getInstance(type, cidNumbers.get(i));
}

@Override
public int getCount() {
return fragmentList == null ? 0 : fragmentList.size();
return subTitles == null ? 0 : subTitles.size();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public interface CommonArticleListContract {
interface View {
void updateArticles(ArticleData data);
void loadDataFail();
}

interface Presenter extends WanAndroidBasePresenter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.reactivex.schedulers.Schedulers;

public class CommonArticleListPresenter implements CommonArticleListContract.Presenter {
private static final String TAG="CommonArticlePresenter";
private static final String TAG = "CommonArticlePresenter";
private CommonArticleListContract.View view;
private CommonArticleListContract.Model model;

Expand Down Expand Up @@ -43,7 +43,10 @@ public void onNext(ArticleDataRes articleDataRes) {

@Override
public void onError(Throwable e) {
Log.e(TAG,"getArticlesFromKnowledges : "+e.getMessage());
Log.e(TAG, "getArticlesFromKnowledges : " + e.getMessage());
if (view != null) {
view.loadDataFail();
}

}

Expand Down Expand Up @@ -77,7 +80,10 @@ public void onNext(ArticleDataRes articleDataRes) {

@Override
public void onError(Throwable e) {
Log.e(TAG,"getArticlesFromProject: "+e.getMessage());
Log.e(TAG, "getArticlesFromProject: " + e.getMessage());
if (view != null) {
view.loadDataFail();
}
}

@Override
Expand Down Expand Up @@ -110,7 +116,7 @@ public void onNext(ArticleDataRes articleDataRes) {

@Override
public void onError(Throwable e) {
Log.e(TAG," getArticlesFromWechatChapter"+e.getMessage());
Log.e(TAG, " getArticlesFromWechatChapter" + e.getMessage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.chejdj.wanandroid.ui.project;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ProgressBar;
Expand All @@ -10,8 +9,6 @@
import com.chejdj.wanandroid.R;
import com.chejdj.wanandroid.network.bean.knowledgesystem.PrimaryArticleDirectory;
import com.chejdj.wanandroid.ui.base.FragmentManagerLazyLoadFragment;
import com.chejdj.wanandroid.ui.base.WanAndroidBaseFragment;
import com.chejdj.wanandroid.ui.commonarticlelist.CommonArticleListFragment;
import com.chejdj.wanandroid.ui.commonarticlelist.CommonPagerFragmentAdapter;
import com.chejdj.wanandroid.ui.project.contract.ProjectContract;
import com.chejdj.wanandroid.ui.project.presenter.ProjectPresenter;
Expand All @@ -33,7 +30,7 @@ public class ProjectFragment extends FragmentManagerLazyLoadFragment implements
@BindView(R.id.progressBar)
ProgressBar progressBar;
private List<String> subTitles;
private List<Fragment> fragmentList;
private List<Integer> cidNumbers;
private static final int TYPE_COMMON_LIST_FRAGMENT = 2;//代表和CommonArticleListFragment的协议

@Override
Expand All @@ -44,7 +41,7 @@ protected int getLayoutId() {
@Override
protected void initView() {
subTitles = new ArrayList<>();
fragmentList = new ArrayList<>();
cidNumbers = new ArrayList<>();
presenter = new ProjectPresenter(this);
}

Expand All @@ -66,12 +63,12 @@ public void updateProjectDirectory(List<PrimaryArticleDirectory> directories) {
progressBar.setVisibility(View.GONE);
}

CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, fragmentList, getChildFragmentManager());
for (PrimaryArticleDirectory projectDirectory : directories) {
subTitles.add(projectDirectory.getName());
cidNumbers.add(projectDirectory.getId());
tabLayout.addTab(tabLayout.newTab().setText(projectDirectory.getName()));
fragmentList.add(CommonArticleListFragment.getInstance(TYPE_COMMON_LIST_FRAGMENT, projectDirectory.getId()));
}
CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, cidNumbers, TYPE_COMMON_LIST_FRAGMENT, getChildFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
tabLayout.setupWithViewPager(viewPager);
Expand All @@ -91,15 +88,6 @@ public void networkError() {
}
}

@Override
public void onDestroy() {
super.onDestroy();
if (!fragmentList.isEmpty()) {
fragmentList.clear();
System.gc();
}
}

@OnClick(R.id.reloadBtn)
public void reloadData() {
if (progressBar.getVisibility() == View.GONE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SubjectArticleActivity extends WanAndroidBaseActivty {
@BindView(R.id.tabLayout)
TabLayout tabLayout;
private List<SecondaryArticleDirectory> secondaryArticleDirectoryList;
private List<Fragment> fragmentList;
private List<Integer> cidNumbers;


@Override
Expand All @@ -47,14 +47,14 @@ protected void initView() {

titleTx.setText(title);
List<String> subTitles = new ArrayList<>();
fragmentList = new ArrayList<>();
cidNumbers = new ArrayList<>();
for (SecondaryArticleDirectory directory : secondaryArticleDirectoryList) {
String directoryName = directory.getName();
subTitles.add(directoryName);
cidNumbers.add(directory.getId());
tabLayout.addTab(tabLayout.newTab().setText(directoryName));
fragmentList.add(CommonArticleListFragment.getInstance(TYPE_COMMON_LIST_FRAGMENT, directory.getId()));
}
CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, fragmentList, getSupportFragmentManager());
CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, cidNumbers, TYPE_COMMON_LIST_FRAGMENT, getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(0);
Expand All @@ -66,12 +66,6 @@ public void back() {
finish();
}

@Override
protected void onDestroy() {
super.onDestroy();
fragmentList = null;
}

public static void startSubArticleActivity(Context context, String title, ArrayList<SecondaryArticleDirectory> detailData) {
Intent intent = new Intent(context, SubjectArticleActivity.class);
intent.putExtra(SUB_TITLE, title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class WechatOfficalFragment extends FragmentManagerLazyLoadFragment imple
@BindView(R.id.progressBar)
ProgressBar progressBar;
private List<String> subTitles;
private List<Fragment> fragmentList;
private List<Integer> cidNumbers;
private static final int TYPE_COMMON_LIST_FRAGMENT = 3;//代表和CommonArticleListFragment的协议

@Override
Expand All @@ -44,7 +44,7 @@ protected int getLayoutId() {
@Override
protected void initView() {
subTitles = new ArrayList<>();
fragmentList = new ArrayList<>();
cidNumbers = new ArrayList<>();
presenter = new WechatOfficalPresenter(this);
}

Expand All @@ -67,12 +67,12 @@ public void updateWechatChapter(List<PrimaryArticleDirectory> data) {
progressBar.setVisibility(View.GONE);
}

CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, fragmentList, getChildFragmentManager());
for (PrimaryArticleDirectory projectDirectory : data) {
subTitles.add(projectDirectory.getName());
tabLayout.addTab(tabLayout.newTab().setText(projectDirectory.getName()));
fragmentList.add(CommonArticleListFragment.getInstance(TYPE_COMMON_LIST_FRAGMENT, projectDirectory.getId()));
cidNumbers.add(projectDirectory.getId());
}
CommonPagerFragmentAdapter adapter = new CommonPagerFragmentAdapter(subTitles, cidNumbers, TYPE_COMMON_LIST_FRAGMENT, getChildFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
tabLayout.setupWithViewPager(viewPager);
Expand All @@ -92,14 +92,6 @@ public void networkError() {
}
}

@Override
public void onDestroy() {
super.onDestroy();
if (!fragmentList.isEmpty()) {
fragmentList.clear();
}
}

@OnClick(R.id.reloadBtn)
public void reloadData() {
if (progressBar.getVisibility() == View.GONE) {
Expand Down
Binary file added app/src/main/res/drawable-xxhdpi/no_data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 37 additions & 7 deletions app/src/main/res/layout/fragment_article_list.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
<android.support.v4.widget.SwipeRefreshLayout

android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

<LinearLayout
android:id="@+id/noData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:visibility="gone"
>

<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/no_data_show"
android:scaleType="centerCrop"
android:src="@drawable/no_data" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_normal"
android:text="@string/no_data_show"
android:textColor="@color/gray"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<string name="wx_friends">朋友</string>
<string name="wx_timeline">朋友圈</string>
<string name="wx_share_error">没有安装微信或网络连接失败</string>
<string name="no_data_show">暂无数据~~</string>
</resources>

0 comments on commit 6fdd2bc

Please sign in to comment.