-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019-09-03:MVP中你是如何处理Presenter层以防止内存泄漏的? #139
Comments
我常用的防止该问题的方法是直接换MVC |
还是换mvvm吧 |
没用过mvp,好像是Presenter持有activity的引用,activity是lifecycle的子类,也是通过lifecycle来释放资源 |
或者直接换mvvm |
在 Activity 执行 onDestroy() 方法之前,调用 Presenter 自定义一个 onDestroy() 方法,因为 Presenter 持有 Activity View 的应用,所以在 Presenter 的 onDestroy() 方法里面把 View = null,在 Presenter 执行 网络请求或者其他的耗时操作,结果返回都必须执行 if(view != null )操作判断。 |
学习了!
[email protected]
发件人: leftcoding
发送时间: 2019-09-03 10:07
收件人: Moosphan/Android-Daily-Interview
抄送: TzuChiangLi; Comment
主题: Re: [Moosphan/Android-Daily-Interview] 2019-09-03:MVP中你是如何处理Presenter层以防止内存泄漏的? (#139)
在 Activity 执行 onDestroy() 方法是,调用 Presenter 自定义一个 onDestroy() 方法,因为 Presenter 持有 Activity View 的应用,所以在 Presenter 的 onDestroy() 方法里面把 View = null,在 Presenter 执行 网络请求或者其他的耗时操作,结果返回都必须执行 if(view != null )操作判断。
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
1 ondestory 手动 view=null,回调的时候 view?.xxx 这是目前最常用的方式吧, 但是 MVC 是大坑吧,不能解决 内存泄漏,反而更加严重才对 |
哈哈哈哈
[email protected]
发件人: YiZe
发送时间: 2019-09-03 10:30
收件人: Moosphan/Android-Daily-Interview
抄送: TzuChiangLi; Comment
主题: Re: [Moosphan/Android-Daily-Interview] 2019-09-03:MVP中你是如何处理Presenter层以防止内存泄漏的? (#139)
1 ondestory 手动 view=null,回调的时候 view?.xxx
2. 利用lifecycle,记不太清了,自行百度吧
3. 换mvvm
这是目前最常用的方式吧, 但是 MVC 是大坑吧,不能解决 内存泄漏,反而更加严重才对
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
我选择 mmp ,MMP yes !! |
@dingyong666 对得起你的名字,如果是我想的那个意思的话=.= |
使用WeakReference 持有V的弱引用,在gc回收扫描到弱引用的时候,就会它回收掉。 Activity onCreate中注册,onStop 解注册。 |
onStop里面取消注册怕是不行吧,切换Acitivity的时候会调用onStop但是换回来的时候并不会调用onCreate.那样岂不是回到原来的Activity没有presenter了? |
首先 MVP 会出现内存泄漏是因为 Presenter 层持有 View 对象,一般我们会把 Activity 做为 View 传递到 Presenter,Presenter 持有 View对象,Activity 退出了但是没有回收出现内存泄漏。 解决办法: |
那就在onDestroy 解注册。 |
Presenter 持有 activity 是lifecycle的子类,通过注册 lifecyclerObserver 监听 distory 方法,取消异步任务。 |
一般使用mvp,都有一个 BaseActivity 或者一个 BaseFragment,而 P 层也有个 BasePresent,我们可在 BasePresent 设置两个方法,onAttach(View view) onDeathView(View view); 在 BaseActivit 的onCreate或 BaseFragment 的onCreateView 使用 onAttach,而在 onDestroy 或者 onDestroyView 使用 onDeathView 解绑;同理,如果 p 层也有使用网络,也可以使用这个方法; |
attachView():在Activity/Fragment执行生命周期方法onCreate()的时候 detachView():Activity/Fragment执行执行生命周期方法onDestroy()的时候 public class GoodsPresenter {
} |
No description provided.
The text was updated successfully, but these errors were encountered: