Aug 16 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Steve S. | has entered the room |
Mark M. |
hello, Steve!
|
Mark M. |
how can I help you today?
|
Steve S. |
Hi, Mark!
|
Steve S. |
I'll paste in a question:
|
Steve S. |
Is there any problem with making an activity singleTop? Is that something I should avoid?
|
Mark M. |
I am not aware of any particular problems
|
Aug 16 | 7:30 PM |
Steve S. |
Ok.
|
Mark M. |
though the key is that the activity has to be at the top of the task
|
Mark M. |
if it is not at the top of the task, AFAIK singleTop has no effect, behaving like standard instead
|
ndrocchietto_ivano | has entered the room |
Mark M. |
that may or may not be a problem, depending upon the various ways that the user might navigate into your app
|
Mark M. |
the _TOP flags (FLAG_ACTIIVTY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP) have a similar limitation, IIRC
|
Steve S. |
I'm not sure I understand. I have an activity I've made singleTop. It's the child of another activity and has children of its own. It does retain values when I return to it from it's children. Is your point that if I were to go to the singleTop activity twice from it's parent, that it would not retain values in that case?
|
Mark M. |
no, that's not what I mean
|
Mark M. |
(BTW, Ivano, I will be with you shortly)
|
ndrocchietto_ivano |
( enjoying...)
|
Mark M. |
given your paragraph, let's say that we have Activity A, B, and C
|
Mark M. |
A starts B, B starts C
|
Steve S. |
ok
|
Mark M. |
B is where your singleTop is
|
Mark M. |
the only place where singleTop plays a role is when something calls startActivity() to start up that activity
|
Mark M. |
so, if the user presses BACK from C to return to B, whether B is singleTop or not does not matter
|
Steve S. |
ok
|
Mark M. |
but, if in C, you call startActivity() to start up B, you might think that singleTop would return you to your existing B instance
|
Mark M. |
however, it shouldn't
|
Aug 16 | 7:35 PM |
Mark M. |
because B is not at the top of the task -- A is
|
Mark M. |
C would create a new instance of B, giving us A -> B -> C -> B in our task
|
Steve S. |
Ok. I think I see what you mean. But that doesn't apply using up navigation in C or calling finish() in C. Is that right.
|
Mark M. |
definitely finish()
|
Mark M. |
er, definitely finish() is not affected by singleTop
|
Mark M. |
up navigation makes my head hurt, so I am uncertain what happens there
|
ndrocchietto_ivano |
( eh eh what a topic)
|
Steve S. |
Ok. My impression at this point is that using either up navigation from C or calling finish in C, B retains it's values if it's singleTop.
|
Mark M. |
if you finish() in C, it does not matter what the launchMode is
|
Steve S. |
ok
|
Mark M. |
it is still the same instance of B regardless
|
Steve S. |
ok - i see
|
Mark M. |
again, launchMode only affects startActivity() calls
|
Steve S. |
ok. I think I understand
|
Mark M. |
I'm hazy as to the conditions when up navigation will use startActivity(), which is why I don't know if singleTop is important there or not
|
Steve S. |
I see.
|
Mark M. |
let me take a question from Ivano, and I will be back with you in a bit
|
Mark M. |
Ivano: your turn! do you have a question?
|
Steve S. |
Sure
|
ndrocchietto_ivano |
yes thanks, Hey Steve!.
|
ndrocchietto_ivano |
I have exceptionally before a metaquestion
|
Steve S. |
hi ivano!
|
Aug 16 | 7:40 PM |
ndrocchietto_ivano |
View paste
|
Mark M. |
I honestly have no idea
|
ndrocchietto_ivano |
:)
|
ndrocchietto_ivano |
OK ball to Steve!
|
Mark M. |
um, OK
|
Steve S. |
I'll paste a follow-up question:
|
Steve S. |
If I pass intent extras to a singleTop activity when starting it, are there any cases where those extras would cease to be available (e.g. if a user navigates away from the activity and then back to it, or if the OS kills the activity due to low memory)?
|
Mark M. |
well, there are extras, and there are extras :-)
|
Steve S. |
ok
|
Mark M. |
in general, extras will not be lost, regardless of launchMode
|
Mark M. |
whatever extras were on the Intent when the activity was created will still be there, regardless of configuration changes and such
|
Steve S. |
right
|
Mark M. |
if the user leaves the app via HOME/RECENTS/something, and the activity is still running, and the app's process is terminated, and the user returns soonish (<30 minutes), so Android re-creates your activity... you still have your extras
|
Steve S. |
ok
|
Mark M. |
if the user leaves the app for an extended period, such that even if the user returns to the app from the overview screen/recent-tasks list, the user will go back to your launcher activity, as if they tapped on the home screen launcher icon
|
Mark M. |
so, in that scenario, extras are "lost", but so is the whole running state of the app
|
Mark M. |
(exception: PersistableBundle on Android 5.0+, though I haven't played with that much)
|
Steve S. |
So in that case it starts all over from the main activity?
|
Mark M. |
yes
|
Mark M. |
now, in the specific case of singleTop, there may be two Intents' worth of extras to consider
|
Aug 16 | 7:45 PM |
Mark M. |
there is the Intent used to create the activity, which you get via getIntent()
|
Mark M. |
however, if you are later called with onNewIntent() -- because your activity was at the top of the task, and some other activity tried starting it -- you get passed in the Intent for that later startActivity() call
|
Mark M. |
this is *not* the Intent returned by getIntent(), which is always the Intent used to create the activity
|
Mark M. |
the Intent used to return to the running singleTop instance of the activity is just passed into onNewIntent(), and it's up to you to hold onto anything in there yourself, such as extras
|
Steve S. |
ok. the case I found where the extras were lost was when i navigated from A to it's child B and then up navigated back to A. If I make A singleTop, I can up navigate back from B to A and the extras are still there.
|
ndrocchietto_ivano |
(shared PReferences...)
|
Mark M. |
unless A was finish()'d, I am not sure how that would have occurred
|
Mark M. |
but, then again, up navigation still makes my head hurt
|
Steve S. |
you mean how the extras would have been lost?
|
Mark M. |
correct
|
Mark M. |
that would imply that up navigation created a fresh instance of A, rather than just returning to the existing instance
|
Mark M. |
and I would have hoped that up navigation was more intelligent than that
|
Steve S. |
ok. then a more general question: what's the best way to insure that the extras used to start an activity will always be available?
|
Mark M. |
hold onto the data yourself somewhere
|
Steve S. |
ok.
|
Mark M. |
for a given activity logical instance, the extras can't be lost, any more than the saved instance state Bundle can be lost
|
Steve S. |
right
|
Mark M. |
if you are finding scenarios where extras are missing, that implies that you have a new logical instance of the activity
|
Steve S. |
ok
|
Aug 16 | 7:50 PM |
Mark M. |
that means one of two things:
|
Mark M. |
1. you need to revise your navigation to not create a new logical instance where you're not expecting it, or
|
Mark M. |
2. you need to save the information somewhere else that lies outside the activity logical instance
|
Mark M. |
some sort of singleton manager object if the data only needs to be in RAM, or in a persistent store if the data needs to survive process termination
|
Mark M. |
personally, I would focus on #1
|
Steve S. |
ok
|
ndrocchietto_ivano |
( too much curious global static variable is bad vs singleton for you?)
|
Mark M. |
Ivano: Android uses statics/singletons more than in classic Java, due to the Android component architecture
|
ndrocchietto_ivano |
OK
|
Mark M. |
the classic issues remain, such as potential memory leaks
|
ndrocchietto_ivano |
I see thanks
|
Mark M. |
we just have to deal with them, rather than enact a blanket ban on statics/singletons
|
ndrocchietto_ivano |
i am a fun of the K values of shared preferences to bring variables trough the classes, are so easy
|
ndrocchietto_ivano |
fan*
|
Mark M. |
yes, but SharedPreferences are persistent, so you are incurring disk I/O along the way
|
ndrocchietto_ivano |
ah ja!
|
ndrocchietto_ivano |
did not think over
|
ndrocchietto_ivano |
so true and inefficient for temporary variable in the buffer
|
Aug 16 | 7:55 PM |
ndrocchietto_ivano |
anyway I would love to ask you my real question
|
Mark M. |
Steve: let me turn to Ivano for a bit, and I'll be back with you shortly
|
Mark M. |
Ivano: go right ahead
|
Steve S. |
sure
|
ndrocchietto_ivano |
my eyes want to sleep in amsterdam.thanks Mark and Steve
|
ndrocchietto_ivano |
ctrl V
|
ndrocchietto_ivano |
View paste
|
Mark M. |
um, I can help you with Android app development questions
|
Mark M. |
for Git advice, you will need to find another expert
|
ndrocchietto_ivano |
OK
|
ndrocchietto_ivano |
OK will try with some code fellow that work in continuos integration
|
ndrocchietto_ivano |
well have a nice night
|
ndrocchietto_ivano |
and see you soon, thanks
|
Steve S. |
you too!
|
Mark M. |
I am sorry that I could not be of more use to you today
|
ndrocchietto_ivano |
I am happy Mark that you are human
|
Mark M. |
Steve: OK, back to you -- do you have another question?
|
Steve S. |
ok. I think I have a better understanding of the situation. I'll simply provide defaults in the cases where the expected intent extras aren't there. That will work for this application.
|
Steve S. |
By the way, I wanted to let you know I just renewed my subscription because you've been so helpful over the past year.
|
Mark M. |
thanks!
|
Steve S. |
Thanks so much, and have a good evening!
|
Mark M. |
you too!
|
Steve S. | has left the room |
Aug 16 | 8:00 PM |
Mark M. |
Ivano: if you are still around and have an Android question, go right ahead
|
Aug 16 | 8:10 PM |
ndrocchietto_ivano | has left the room |
Aug 16 | 8:25 PM |
Mark M. | turned off guest access |