-
Notifications
You must be signed in to change notification settings - Fork 253
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
When to destroy activity's presenter #82
Comments
if an activity may not restart forever,why not leak the background tasks? |
Because some background tasks can be very resource-consuming (streaming, image processing, etc). |
I think the system will drop an activity only after a certain time( |
Android never drops activities after some time alone. It drops them only with the entire process (together with background tasks). |
No,If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process |
Do you have any references to docs or tests? I created a test once which runs 100500 activities one on top of another - just to check if Android will finish some of background Activities when it becames short on memory. It didn't. |
There is also a discussion on stackoverflow somewhere with the same conclusion. |
Yes, doc I think the system drop the activity in this situation because of time but not memory. |
:D such an obvious lie in official docs. I never had a sutuation when Android finishes my Activities without my own command. It can finish them indirectly if I say FLAG_ACTIVITY_CLEAR**, but it never finishes them by itself. How much time do you think we need for activity to be finished because of time? |
I've found something interesting: http://developer.android.com/guide/components/tasks-and-back-stack.html "If the user leaves a task for a long time, the system clears the task of all activities except the root activity. " |
Ok,It said 30 minutes .I happened to see it before and I forget where,you find it. |
It is always nice to know more info about these lifecycles, thanks for mentioning them! :) |
Hey, did you red this article? i think it is really nice way to bound to Activity lifecycle. |
@bejibx I hope you're kidding. This ugly workaround using one of the most buggy Android APIs will never make its way to my code. Never. |
Workaround is kind of ugly, true, but it seems to me pretty much every possible solution to beat Activity lifecycle is ugly. Still I'm a bit confused, because loaders seems to work like a charm in my projects before. |
@bejibx you're lucky then. Their lifecycle is not the same as Activity has and not the same as process has. So, they obviously add complexity instead of reducing it. And, in addition to the added complexity, it has so many callbacks and unneeded API complexities like sending arguments in a Bundle, forceLoad... they just suck. |
Nucleus exists only because loaders do not work. |
I got your point and I'm definitely agree with you about Loaders API over-complexity. I hope I'll have some time on this weekends to investigate Android Activity and Loader sources to see if system somehow diferentiate restart from complete destroy. |
44c7722 do not fix the situaction in case 2 "restart-activity" in your post.In that situation,the method |
@duo2005duo Which post do you mean? |
This one? "Case 2: An Activity restart happens when a user has set "Don't keep activities" checkbox in Developer's settings and another activity becomes topmost." ? |
@konmik Yes |
I think it will have |
@konmik this is true, just checked it in sample project. |
Of course.But do you think the retained fragment lifecycle is similar to current presenter lifecycle?Combining staticholder with saveInstanceState just do the same with retained fragment.I think it voliate KISS. |
And I think the loader lifecycle is the same with retain fragment.They are both |
@duo2005duo the idea is to not have the same background task running twice. Without having a static variable we can't detect if the task is still running. Ratained fragments do not fit because they get destroyed during "case 2". |
Yes,But I think case 2 is not so important,The user will not do this except the developer,so I said "similar". And it still need to be tested whether it will have isFinishing during its next onDestroy for cleaning stack we talked about yesterday |
And I think it will not have its fininishing,the activity will not destroy twice without have a second onCreate. |
@duo2005duo You're right this is not very important. But why complicate things and use fragments if we can do the same without them? Fragments is not the only thing we need presenters for. It will have a second |
The result is interesting !:-D |
Also I found this mentioned earlier stackoverflow conversation about Android destroying activities. So I think when lead Android engineer says
we can trust him. |
I think I found this rare case when we can leak our presenters because |
@bejibx You're right, we will have a leak in this case. I was already thinking about this. I think we need to recommend |
While this flag doesn't help with "Do not keep Activities" it will help with Android finishing activities by itself. |
But did you test if Also I tested what is gonna happen with flag |
Also just found this interresting stackoverflow article. |
final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
ActivityRecord newActivity) {
boolean forceReset =
(newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
if (ACTIVITY_INACTIVE_RESET_TIME > 0
&& taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
forceReset = true;
}
}
//... If we look at ActivityStack#resetTaskIfNeededLocked(ActivityRecord, ActivityRecord) function we can see that no matter why activity should be killed - because of inactive reset time came off or because |
I'm thinking about retained fragments and |
Here is a possible implementation, BTW: https://github.com/square/flow/blob/master/flow/src/main/java/flow/InternalLifecycleIntegration.java I need to test retained fragments over edge cases. |
Calm down for a moment, we just need to test first what happens with Activity when system destroys it because of time. It is confirmed this could be easily reproduced on Android 2.3:
I'll test this on emulator tonight or tomorrow if I there is no time today and report results. |
@bejibx Even if it will call |
moving to retained fragments will also automatically solve fragment's presenter lifecycle problem where we need to manually destroy presenter. |
We will have sometimes several instances of the same background task running but it will, at least, be a predictable scenario every other android developer is facing. |
@konmik The RxJava will stop the unneccessary task instances as soon as possible with the help of |
Hey, can we use isChangingConfigurations() instead of isFinishing()? So presenter will NOT be destroyed only if it is configuration change incoming. |
@bejibx awesome. I will try it. |
3.0.0-beta is on maven |
We need a more reliable way to detect when to destroy activity's presenter.
Here is where the issue that started the discussion: #42 (comment)
TL/DR: activity's
finish()
can be called some time afteronPause
, so this code will leak the activity's presenter: https://github.com/konmik/nucleus/blob/master/nucleus/src/main/java/nucleus/view/NucleusActivity.java#L76Possible solutions:
a) Leverage retained fragments. This will solve the presenter destruction issue but we will leak background tasks sometimes.
b) Extend
PresenterLifecycleDelegate
lifecycle to explicitly callonDestroy
.c) Install system-wide lifecycle listener
registerActivityLifecycleCallbacks
.The text was updated successfully, but these errors were encountered: