Apr 16 | 8:55 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Apr 16 | 9:00 AM |
trocchietto_Ivano | has entered the room |
Mark M. |
hello, Ivano!
|
Mark M. |
how can I help you today?
|
trocchietto_Ivano |
Hallo Mark!
|
trocchietto_Ivano |
I have a question about my app. How can I make sure that on older devices with low memory my view models don't get destroyed when the activity calls onDestroy function
|
Mark M. |
is onDestroy() being called as part of a configuration change? or is the activity being finished?
|
Eric | has entered the room |
Eric |
hi everyojne
|
Mark M. |
hello, Eric -- I will be with you shortly!
|
trocchietto_Ivano |
Well, it happens when I open an Image or File picker. When that activity starts my activity calls onDestroy. It's not a configuration change.
|
Eric |
no problem
|
trocchietto_Ivano |
Ciao Eric
|
Mark M. |
is the image or file picker in another app?
|
Apr 16 | 9:05 AM |
trocchietto_Ivano |
No in my app.
|
trocchietto_Ivano |
but it opens the Android gallery or Camera
|
Mark M. |
"the Android gallery or Camera" are other apps than yours, though
|
trocchietto_Ivano |
oh, well, in that case yes.
|
Mark M. |
OK, there are three possibilities that I can think of:
|
Mark M. |
1. you have that Developer options setting for "don't keep activities" turned on
|
Mark M. |
2. your process is being terminated due to low memory conditions
|
Mark M. |
3. you are finishing the activity yourself as part of navigating to the other app
|
Mark M. |
in all three of those cases, your viewmodel is supposed to go away
|
trocchietto_Ivano |
Ahh that 2nd one can indeed be the problem now that I think of it.
|
Mark M. |
and in case #2, you don't have a choice, as your whole process is going away
|
trocchietto_Ivano |
let me check
|
Mark M. |
this is common on low-memory devices with the camera, as camera apps tend to consume a lot of memory
|
Mark M. |
you cannot keep your viewmodel around, but you can put information in the saved instance state Bundle
|
Mark M. |
and if you are using Google's ViewModel, there is stuff coming up that will help ViewModel integrate with the saved instance state Bundle
|
trocchietto_Ivano |
One thanks for the clarification
|
trocchietto_Ivano |
I go to test
|
Mark M. |
let me take a question from Eric while you are testing, and I'll be back with you shortly
|
Mark M. |
Eric: your turn! how can I help you today?
|
trocchietto_Ivano |
thank you
|
Eric |
I have a button that makes a custom bottom sheet appear. Are bottom sheets supposed to be able to appear by swiping up also? Mine cannot do that
|
Apr 16 | 9:10 AM |
Mark M. |
bottom sheets usually can be expanded or collapsed via swipe gestures, though I think that's configurable
|
Mark M. |
I haven't used Google's bottom sheet stuff much, so I am fuzzy on the details
|
Eric |
im confused about if that's default behavior or not. I read it is swipeable by default but mine can swipe down only
|
Mark M. |
is it already open to the "expanded" height at the outset?
|
Eric |
if I tap outside the bottomsheet, or slide it down, does that dismiss the bottomsheet and is memory reclaimed
|
Mark M. |
if the bottom sheet is a dialog, then yes, that should dismiss the dialog
|
Mark M. |
if the bottom sheet is a view in your layout, then AFAIK it is still there in the view hierarchy
|
Eric |
the bottom sheet is in another fragment
|
Eric |
if it's dismissed I don't think it's possible to bring it back by swiping up
|
Mark M. |
agreed
|
Mark M. |
AFAIK, a regular view bottom sheet has collapsed and expanded heights, and by default the user can swipe between those states
|
Apr 16 | 9:15 AM |
Mark M. |
the only "dismiss" scenario that I know of is for BottomSheetDialog/BottomSheetDialogFragment, where the "dismiss" behavior is tied to it being a dialog, not it being a bottom sheet
|
Eric |
cool mine extends from BottomSheetDialogFragment
|
Mark M. |
right, so when the user dismisses it, it's gone forever
|
Mark M. |
that's no different than any other dialog, whether wrapped in a DialogFragment or not
|
Eric |
do you need to explicitly call dismiss?
|
Mark M. |
if you are looking for the user to be able to expand and collapse the bottom sheet repeatedly, I think you need a non-dialog bottom sheet
|
Mark M. |
I think by default clicking outside the bottom sheet dialog would dismiss the dialog, though I think that is configurable
|
Eric |
it does dismiss it but i was wondering about memory cleanup
|
Mark M. |
once the dialog is gone, memory associated with it should get garbage-collected, barring some bug somewhere
|
Mark M. |
again, that's not unique to bottom sheet dialogs
|
trocchietto_Ivano |
(interesting indeed the internals of Android)
|
Mark M. |
Eric: let me take another question from Ivano, and I'll be back with you shortly
|
Mark M. |
Ivano: back to you! do you have another question?
|
trocchietto_Ivano |
uncle bob is right when he says a dev should learn assembly as fast as possible, and let the magic disappear
|
Apr 16 | 9:20 AM |
Eric |
good to know. i was considered that if you called newInstance for every button click you would leak memory but it sounds like if you can't bring it back it's dismissed and its memory eligible for GC
|
trocchietto_Ivano |
HI Mark thank you very much is working as a charm
|
Mark M. |
(Eric: correct, though consider Leak Canary or something to confirm you're not accidentally holding onto something that prevents the fragment from being GC'd)
|
trocchietto_Ivano |
it was the first case, my senior activated the developer option to not keep activities
|
Mark M. |
ah, OK
|
Mark M. |
particularly for a file picker, I would be a bit surprised for your process to be terminated quickly
|
Eric |
ok thanks mark. bye
|
Eric | has left the room |
Mark M. |
personally, I almost never turn on that "don't keep activities" setting, just because of cases like this where I will forget that I have it enabled
|
trocchietto_Ivano |
I see managing hold phones can be a pain sometimes
|
Mark M. |
sorry, but I am uncertain what "managing hold phones" means
|
trocchietto_Ivano |
absolutely understandable your choice, I experienced that on my skin today, was stuck more than 4 hours
|
trocchietto_Ivano |
well getting a lot of bugs from phones a bit older, in particular motorola
|
trocchietto_Ivano |
but I guess it depends by the code that was not good written
|
trocchietto_Ivano |
anyway you have been already too kind
|
trocchietto_Ivano |
I wish you a pleasant day, thank you are a living lifesaver
|
Mark M. |
I'm happy to be useful!
|
trocchietto_Ivano |
you definitely are and so are your books
|
trocchietto_Ivano |
bye
|
Apr 16 | 10:00 AM |
trocchietto_Ivano | has left the room |
Mark M. | turned off guest access |