我最近使用ViewPager创建了一个应用程序,我的子片段没有被正确销毁,导致内存不足异常。我使用此https://github.com/adamsp/FragmentStatePagerIssueExample/blob/master/app/src/main/java/com/example/fragmentstatepagerissueexample/app/FixedFragmentStatePagerAdapter.java
修复了它现在我已经重建了我的应用程序以使用控制器而不是碎片而且事情变得混乱。 Conductor库通过ControllerPagerAdapter获得ViewPager的官方支持,但它看起来像原来的android一样破坏了。这是我的代码:
我在PagerController中创建适配器:
PagerAdapter adapter = new PagerViewAdapter(this, pdfFactory.getPageCount());
PagerViewAdapter:
public class PagerViewAdapter extends ControllerPagerAdapter {
private final int size;
public PagerViewAdapter(Controller controller, int size) {
super(controller, false);
this.size = size;
}
@Override
public int getCount() {
return size;
}
@Override
public Controller getItem(int position) {
return new PagerImageController(position);
}
}
PagerImageController:
public class PagerImageController extends Controller {
@Inject PdfFactory pdfFactory;
private int position;
public PagerImageController() {}
public PagerImageController(int position) {
this.position = position;
setRetainViewMode(RetainViewMode.RELEASE_DETACH);
Log.d("LOG", "PagerImageController: "+position);
}
@NonNull
@Override
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
//Dagger 2
((CorePdfApplication) getActivity().getApplication()).getComponent().inject(this);
//Locate and inflate the PhotoView
final View v = inflater.inflate(R.layout.pager_element, container, false);
PhotoView photoView = (PhotoView) v.findViewById(R.id.pager_image);
Observable.just(position)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(integer -> {
photoView.setImageBitmap(pdfFactory.getPage(integer));
})
.subscribe();
return v;
}
}
LOG:
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:114)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 20866572 byte allocation with 16777216 free bytes and 18MB until OOM
任何想法如何解决这个问题?