Comments (12)
@gengjiawen
android.widget.Toast$TN.mNextView leak solution is
using
Toast.makeText(context.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
instead of
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
from androidmvp.
I understand the point behind this issue, because it could lead to problems that the view keeps attached after the activity has been destroyed, but I don't see how to reproduce the leak you're mentioning.
The MainActivity will actually leak memory when I go back to LoginActivity while the presenter of the MainActivity is fetching data in background
This can't happen afaik, because you can't go back from MainActivity
to LoginActivity
, it will close the App. The activity will probably be leaked while the background task is being done, but it'll be released afterwards.
I track the memory leak using leakcanary, and MainActivity do leaked.
I added LeakCanary to the project, and was navigating for a while, but couldn't find a way to leak it. The message @gengjiawen is telling that is being leaked by a toast. I'm not very used to LeakCanary, so could be a reason why I don't find it. If you could please create a PR with LeakCanary included and a way to reproduce it, it'd be helpful.
Apart from that, it's true that the example is not perfect. The listener shouldn't be called if the activity is destroyed, or at least the activity shouldn't be updated. But background tasks shouldn't use handlers either, this example is just a simplification. Anyway, I'll look for the simplest possible way to fix this issue.
from androidmvp.
I have detect this by calling GC manually. The MainActivity will actually leak memory when I go back to LoginActivity while the presenter of the MainActivity is fetching data in background.
In my case, I will remove reference of IView in the Presenter when the Activity is onDestroy.
from androidmvp.
I track the memory leak using leakcanary, and MainActivity do leaked.
* com.antonioleiva.mvpexample.app.main.MainActivity has leaked:
* GC ROOT android.widget.Toast$TN.mNextView
* references android.widget.LinearLayout.mContext
* leaks com.antonioleiva.mvpexample.app.main.MainActivity instance
from androidmvp.
Thanks! Feel free to PR a solution, otherwise I'll try to find some time to fix this at some point.
from androidmvp.
@kaedea so you would do loginview = null when activity is destroyed?. Or may be using a weakreference might help??
from androidmvp.
@kaedea i did take a look at your implementation of mvp @ https://github.com/kaedea/android-mvp-pattern/blob/master/app/src/main/java/me/kaede/mvp/loginoptimized/presenter/LoginPresenterCompl.java where in you have iLoginView = null .
from androidmvp.
@antoniolg you are right. Thanks for the update.
from androidmvp.
@antoniolg In MainActivity just rotate the screen it will reproduce the bug if I remember correctly.
From the experience I deal with memory leak, "leaked by a toast" is mostly indicated leak an activity instance, which is very common.
I will test you fix on this issue when I got time :)
from androidmvp.
@openproject you are wrong, this is not the case.You leak a context, it will show leak log like that, it's has nothing to do with the Toast.
@antoniolg I tested on new code,the leak no longer exists:)
from androidmvp.
Thanks for testing it @gengjiawen !!
from androidmvp.
can someone post a solution for this problem? Thanks
from androidmvp.
Related Issues (18)
- Unit tests HOT 6
- Orientation Change HOT 2
- More advanced use cases HOT 6
- how make request for one api HOT 2
- Import this project HOT 2
- Scrren rotation HOT 2
- There is a loose coupling between View and Model in main package
- I'd like to create Mockito tests for the LoginPresenterImpl HOT 3
- Is this package structure correct? HOT 6
- Interactor what in MVP HOT 11
- Interactor being instantiated in the View HOT 4
- [Question] Which is better using contact? or dividing presenter between view interface? HOT 3
- Where to reach domain layer from MVP HOT 2
- DI with your MVP example HOT 2
- LoginInteractor中的一个小问题 HOT 2
- I have a problem in gradle build
- How about life cycles ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidmvp.