Office Hours — Today, June 20

Thursday, June 15

Jun 20
3:50 PM
Mark M.
has entered the room
Mark M.
turned on guest access
4:20 PM
Lucy
has entered the room
Lucy
Hi Mark, how are you?
Mark M.
hello, Lucy!
how can I help you today?
Lucy
Let's see...I have a couple of questions regarding navigating
4:25 PM
Lucy
I guess my first question is: is there a trick to using addToBackStack when adding Fragments in a transaction? It doesn't seem to do anything for me. Hit the Back button on the device and I go back to the first Fragment.
Mark M.
I guess I do not understand your question
addToBackStack() reverses the transaction when the user presses BACK
Lucy
I beginTransaction and then addToBackStack. But clicking Back does not take me back to the last Fragment in the series of Fragments in the Activity such as: F1->F2->F3 and then hit Back. I go to F1.
Mark M.
are F2 and F3 being added in a single transaction, or in multiple transactions?
Lucy
Multiple Transactions. User clicks Next in a Fragment and I create a new transaction and add or replace the existing fragment with a new one.
Mark M.
and both of those transactions have addToBackStack()?
Lucy
that's correct
Maybe I am still doing something wrong. If it should "just work" then I will try again.
Mark M.
and both transactions are applied by the same FragmentManager?
it should "just work"
Lucy
Yes. In fact, I replace/add the Fragments inside the Activity by calling mListener.switchMyFragment() kind of design.
Mark M.
then I cannot explain your results
Lucy
I will try again.
OK next navigation topic!
4:30 PM
Lucy
I've read page 421 of Busy Coders and I also saw some SO posts of yours regarding use of the flags: FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP
In my case, I have the following progression of Activities: A->B->C
On Activity C the user can click one of several buttons that launch other Activities let's call them D1->E1 and D2->E2. Ideally I'd like to return to Activity C.
My question is: can I use those flags to return to any Activity I want in the progression?
Mark M.
FLAG_ACTIVITY_CLEAR_TOP should do that; also use FLAG_ACTIVITY_SINGLE_TOP if you want to use the existing C instance
otherwise, assuming you haven't messed with launchOptions in the manifest, C will be destroyed and recreated
Lucy
Yes that is what I would like. So for A->B->C I pass no flags. Then for D1, E1 etc I pass those flags?
Mark M.
no
when you want to go back to C, you pass those flags
Lucy
Yeah I tried using those manifest options. I will make sure they are all gone.
Mark M.
on your startActivity() that returns the user to C
4:35 PM
Lucy
OK cool I will try that!
Mark M.
the docs for FLAG_ACTIVITY_CLEAR_TOP are pretty close to describing your scenario: https://developer.android.com/reference/android...
Lucy
View paste
Hm. Maybe I had a stray android:launchMode="singleTop" in my manifest which made things not work for me.
Excellent, thanks!
I will study that flag further. Perhaps I should control things from the code rather than the manifest.
Mark M.
I prefer to control from code where I can, particularly for non-exported activities
Lucy
I'll polish my navigation further as its a bit clunky right now.
Mark M.
you have greater flexibility
as there is but one manifest
yeah, navigation in a multi-activity app can get painful
Lucy
great point.
Yeah I wrote those bits to be Activities because I might want the functionality to be called from somewhere else.
Mark M.
if by "somewhere else", you also mean "someONE else", then you need to think more about launch modes
as you cannot readily force a third-party app to use certain flags
Lucy
Oh luckily I mean in my own app.
Mark M.
OK
Lucy
Only myself calling them. They bring up a piece of data that can be handy on its own.
4:40 PM
Lucy
Another question. I hope it makes sense. I am sending a notification to the user. They tap it in the notification bar, my app opens ActivityXYZ. I want the user to have a one-time chance to do something in response. I don't have navigation to ActivityXYZ except when tapping a notification. I wondered if its enough to enforce the "one time" whereby user taps, opens ActivityXYZ, clicks a button and I call finish() on that button.
4:40 PM
Lucy
Is it enough to make something a "one time" opportunity to do XYZ in response to a Notification.
I suppose I am asking that without another way to navigate to ActivityXYZ they should not be able to call it.
Mark M.
well, users cannot get to any activity unless you provide a means for them to do so
Lucy
s/call it/launch it/
Mark M.
what you need to do is ensure that XYZ does not wind up in its own task
Lucy
OH.
Mark M.
as otherwise the user could get back to that task from the overview screen
and notifications historically required FLAG_ACTIVITY_NEW_TASK
Lucy
I see. Interesting. I will research that.
Mark M.
or, if you really do need it in its own task, try finishAndClearTask()
Lucy
I've been using an adb command to track how many activities are open.
Oh I'm not familiar with finishAndClearTask(). Will read about that.
Mark M.
sorry, finishAndRemoveTask()
new to API Level 21, though
Lucy
Hm. Yeah my app will support 19+
Still, I did not know about this. I will read more.
4:45 PM
Lucy
When I figure out what I'm doing wrong for addToBackStack I'll come back and let you know! Ditto for Activity flags.
Thanks again!
Mark M.
you're welcome!
Lucy
has left the room
4:55 PM
Mark M.
turned off guest access

Thursday, June 15

 

Office Hours

People in this transcript

  • Lucy
  • Mark Murphy