您的位置:首页 > 移动开发 > Android开发

了解关于Android内存泄露等相关问题的处理方式

2018-02-11 11:49 423 查看
我们在开发过程中,不时会遇到一些内存泄露等问题,比如说,打开一个Activity,会findViewById到一些需要的控件来进行ADUS等操作。。
        而当我们在结束一些Activity或Fragment之后,之前findViewById到的一些控件仍然存在于内存中,当执行到特定场景时,就会出现空指针等一些内存泄露问题,这时候,我们的LiveDataReactiveStreams就发挥作用的时刻到啦,相关参考资料可以自行上网查询,我这里只粘贴部分资料作了解:
参考资料

RxJava and Lifecycles

RxJava is cool, albeit confusing. But beyond that, it is a pure Java library. RxJava knows nothing about Android-specific concepts, as it is designed to be used on all sorts of Java projects.Android developers using RxJava invariably also add RxAndroid, which gives us access to a 
Scheduler
 that knows about the Android main application thread. However, RxAndroid does not have anything that deals with activity or fragment lifecycles, leaving that up to you. With Android lifecycles, we want to create things as activities and fragments start up and clean up those things as the activities and fragments go away. In the case of RxJava, if we subscribe to some 
Observable
, it would be nice to get rid of that subscription at an appropriate point.In this chapter, we will explore a few options — including one from the Architecture Components — for dealing with lifecycles with RxJava.

谷歌原文

LiveDataReactiveStreams

public final class LiveDataReactiveStreams 

extends Object 
java.lang.Object
   ↳android.arch.lifecycle.LiveDataReactiveStreams
Adapts 
LiveData
 input and output to the ReactiveStreams spec.具体使用可以按如下写法:
LiveData<List<RecommendData>> liveData = LiveDataReactiveStreams.fromPublisher(RetrofitUtil.create(StatisticsApi.class, UrlConstants.getUrlConfig().getUrl() + lotteryType + "/")
.getRecommendData(new RequestContent.Builder<List<RecommendData>>().build())
.compose(new HttpResultFlowableTransformer<>()));
liveData.observe(this, new Observer<List<RecommendData>>() {
@Override
public void onChanged(@Nullable List<RecommendData> recommendDataList) {
getRecommenData(recommendDataList);
}
});用于更新数据,我们就无需再管内存会不会泄露,view等控件的回收问题啦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: