Jul 3 | 8:55 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jul 3 | 9:10 AM |
EGHDK | has entered the room |
Mark M. |
hello, EGHDK
|
Mark M. |
how can I help you today?
|
Jul 3 | 9:15 AM |
EGHDK |
Hey Mark. So basically, I haven't gotten to reading about singletons in my book yet (up to File I/O), but I found myself having to create a singleton yesterday. I have a list that gets sorted in my application every activity I go to. The problem is that it takes time.
|
EGHDK |
So I just created this in the application level, and now I retrieve it from there. And it works very well!
|
EGHDK |
No more delays when switching activities.
|
EGHDK |
My problem lies in the fact, that I would like to "delete" that list every time I leave.
|
Mark M. |
define "delete" and "leave"
|
EGHDK |
So like null it out in the application class, when I press the home button.
|
EGHDK |
Just leave the application in general.
|
EGHDK |
But I guess that's not possible right?
|
Mark M. |
what is your objective for doing this?
|
EGHDK |
Theres no easy way in android to tell whether you're inside or outside the application?
|
EGHDK |
My objective is that the dataset might have been changed, and I want to resort the data when you come back into the app.
|
Mark M. |
then why not resort the data when you com back into the app?
|
Mark M. |
er, come back into the app?
|
Mark M. |
the criteria for needing a resort is not "the user pressed HOME"
|
Mark M. |
the criteria for needing a resource is "the dataset might have been changed"
|
EGHDK |
The data is from the contacts, and I sort it in my app class, but when I leave and come back I would like to resort, because it might have changed.
|
Mark M. |
and that can occur at any moment
|
EGHDK |
This is true.
|
EGHDK |
How would I do that though?
|
Jul 3 | 9:20 AM |
Mark M. |
the key isn't whether you are in the foreground or the background, but whether your process is running or not
|
EGHDK |
"come back into the app?" Is that possible? To know when a user has come back into the app?
|
Mark M. |
have whatever code you are using to retrieve and sort these contacts register a ContentObserver to be notified about changes in the contacts
|
Mark M. |
when the ContentObserver says there has been a change, reload and resort
|
EGHDK |
Great.
|
Mark M. |
you will not find out about such changes when your process is not around, but you need to reload the data again anyway, as your singleton went *poof*
|
Mark M. |
BTW, on a completely unrelated note, I just found out that I will be needing to end the chat in probably 15-20 minutes -- I have a plumbing issue, and the plumber is on his way
|
EGHDK |
Oh no!
|
EGHDK |
Alright, just tell me when you need to leave and I'll be off!
|
Mark M. |
will do
|
Mark M. |
in the meantime, go ahead with your questions
|
EGHDK |
Once my process is not around... then my contentobserver won't be around right?
|
EGHDK |
And my singleton won't be around either...?
|
Mark M. |
correct on both counts
|
EGHDK |
So then my application oncreate will be called again...
|
EGHDK |
If I enter my app since the process was killed?
|
Mark M. |
correct
|
Jul 3 | 9:25 AM |
EGHDK |
So let me get this completely straight though... there is no way to know if I'm inside my own application or not?
|
Mark M. |
I do not know what "I'm inside my own application or not" means
|
EGHDK |
Inside an activity. sorry.
|
Mark M. |
not reliably
|
Mark M. |
usually, you solve the problem in some other way
|
EGHDK |
I'm confused, because some apps seem to use that kind of data... In spotify for example if I'm inside any activity the MediaPlayer Notification is not in the notification tray. Then right when I hit the home button, the notification comes in... and if I pull down then the MeidaPlayerNotification is there.
|
EGHDK |
(Not sure if MediaPlayerNotification is real) but its just a notification with play//pause
|
Mark M. |
that's because they are solving the problem in some other way
|
Mark M. |
or they are relying upon onUserLeaveHint(), which is not guaranteed to be reliable
|
EGHDK |
Like... onStop being called they throw up the notification... and on onResume of another activity they remove it. That could happen fast enough I guess.
|
EGHDK |
Why is onUserLeaveHint not reliable?
|
Jul 3 | 9:30 AM |
Mark M. |
there is no guarantee that it will be called
|
EGHDK |
Erg... that's arggravating.
|
Mark M. |
for example, it is not called when a phone call comes in, and the in-call screen takes over the foreground
|
EGHDK |
If I wanted to do the same thing... with a media player app that I made... what would be the best bet...?
|
Mark M. |
that being said, reading the JavaDocs, for the express purpose of maintaining a Notification, Google advises onUserLeaveHint() and onUserInteraction()
|
EGHDK |
So where do I find out that onUserLeaveHint is not reliable?
|
Mark M. |
you read the JavaDocs
|
Mark M. |
"Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback. "
|
Mark M. |
"This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication."
|
Mark M. |
so, again, for the Spotify scenario of showing a notification, onUserLeaveHint() may be "good enough"
|
Mark M. |
but, for example, suppose that the reason you wanted to null out your sorted data is to save on memory
|
Mark M. |
for that, I'd implement onTrimMemory() in the component that is managing the sorted data (Application in this case, as I understand it)
|
Mark M. |
and clear out memory based upon the parameter passed to onTrimMemory()
|
Jul 3 | 9:35 AM |
EGHDK |
Thanks... That's exactly the question I had in my mind. Just didn't know how to write it out.
|
EGHDK |
Super!
|
EGHDK |
There's probably no certainty that onTrimMemory() will get called though... This is android... after all.
|
Mark M. |
no, but up until your process is terminated, you should get some calls
|
Mark M. |
you may not get any specific flag value as a parameter
|
Mark M. |
you're called at several points in time, in increasing desperation :-)
|
Mark M. |
and the plumber is here
|
Mark M. |
I'll post the transcript for this sometime later this morning, when I get a chance
|
Mark M. |
the next chat should be Tuesday at 7:30pm US Eastern Time
|
Mark M. |
have a pleasant day!
|
EGHDK | has left the room |
Mark M. | turned off guest access |