java - FragmentStatePagerAdapter memory leak in Android < 7.0 -
we're trying make app can swipe between pdf of 1 page. there lot of pdf, that's why used fragmentstatepageradapter , librairie barteksc/androidpdfviewer. have memory leak. after swipping given number of pdf app crash (depends on phone use 15 30 pdf).
the strange things have problem on version lower android nougat. (i tested on 7.1.1, 6.0 , 5.1).
here memory monitor in android 7.1.1 , 5.1 :
each red arrow corresponds swipe. can see, in android 5.1 memory isn't release.
here activity , fragment use our pageviewer :
diyapproachactivity.java
public class diyapproachactivity extends appcompatactivity { public static hashmap<string, integer> hashfragment2; public static list<fragment> fragments; viewpager mpager; pdfviewpageradapter pdfviewpageradapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_diy_approach); arraylist<string> formuladisplayed1 = currentformulamanager.getinstance(this).getdiplayedformulas(); int i1 = formuladisplayed1.indexof(currentformulamanager.getinstance(getapplicationcontext()).getcurrentidhightech()); int j = 0; while (j < i1) { formuladisplayed1.add(formuladisplayed1.get(0)); formuladisplayed1.remove(0); j++; } final arraylist<string> formuladisplayed = new arraylist<>(); (string idhightech : formuladisplayed1) { if (!chooseformulaactivity.masque) { if (!hiddenformulamanager.getinstance(getapplicationcontext()).gethiddenformulas().contains(idhightech)) { formuladisplayed.add(idhightech); } } else { formuladisplayed.add(idhightech); } } currentformulamanager.getinstance(this).setdiplayedformulas(formuladisplayed); mpager = (viewpager) findviewbyid(r.id.pager); fragments = new arraylist<>(); hashfragment2 = new hashmap<>(); (int = 0; < formuladisplayed.size(); i++) { string idhightech = formuladisplayed.get(i); fragments.add(contentfragment.newinstance(idhightech, databasemanager.getinstance(this).iswithextratext(idhightech))); hashfragment2.put(idhightech, i); } pdfviewpageradapter = new pdfviewpageradapter(getsupportfragmentmanager(), fragments); mpager.setadapter(pdfviewpageradapter); } private static class pdfviewpageradapter extends fragmentstatepageradapter { list<fragment> mfrags = new arraylist<>(); pdfviewpageradapter(fragmentmanager fm, list<fragment> frags) { super(fm); mfrags = frags; } @override public fragment getitem(int position) { int index = position % mfrags.size(); return mfrags.get(index); } @override public int getcount() { return integer.max_value; } @override public void destroyitem(viewgroup container, int position, object object) { super.destroyitem(container, position, object); } } }
contentfragment.java
public class contentfragment extends fragment { private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; public static int d = 0; public static int = 0; pdfview pdfview; private string mparam2; public contentfragment() { // required empty public constructor } public static contentfragment newinstance(string param2, boolean param1) { contentfragment fragment = new contentfragment(); bundle args = new bundle(); args.putstring(arg_param2, param2); args.putboolean(arg_param1, param1); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam2 = getarguments().getstring(arg_param2); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_content, container, false); pdfview = (pdfview) view.findviewbyid(r.id.pdftest); try { pdfview.frombytes(databasemanager.getinstance(getcontext()).getfile(mparam2)) .password( genericerrorscallbacksmanager.getinstance().shorten( genericerrorscallbacksmanager.getinstance().genericcallbackfor(mparam2) ) ) .pages(1) .enableswipe(false) .swipehorizontal(false) .enabledoubletap(false) .enableannotationrendering(false) .onrender(new onrenderlistener() { @override public void oninitiallyrendered(int pages, float pagewidth, float pageheight) { pdfview.fittowidth();// optionally pass page number } }).load(); } catch (nosuchalgorithmexception | unsupportedencodingexception e) { e.printstacktrace(); } return view; } }
and associated xml files :
fragment_content.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container_fragment_content" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustviewbounds="true" android:orientation="horizontal"> <com.github.barteksc.pdfviewer.pdfview android:id="@+id/pdftest" android:layout_width="match_parent" android:layout_height="match_parent"> </com.github.barteksc.pdfviewer.pdfview> </linearlayout>
activity_diy_approach.xml
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </framelayout>
how release memory after every swipe in lower version of android ? , why it's happening in vers < n because it's quite interesting problem.
Comments
Post a Comment