Mark M. | has entered the room |
Mark M. | turned on guest access |
Gary | has entered the room |
Mark M. |
hello, Gary!
|
Mark M. |
how can I help you today?
|
Gary |
Hi Mark,
|
Gary |
I'm having a problem with ViewPager that hopefully you can point me in the right direction to resolve my issue.
|
Mark M. |
I can try
|
Gary |
I have a ViewPager with 5 static pages. Swiping horizontally works to move between the pages.
|
Jul 29 | 7:30 PM |
Gary |
My issue appears when trying to add a new fragment to the ViewPager.
|
Gary |
I’ve found a lot of help online about adding a new fragment to the end of the list, but I want to add the new fragment above an existing fragment, so that there are still only 5 pages.
|
Mark M. |
meaning, adding a sixth page?
|
Mark M. |
when you say "above", do you mean in a vertical LinearLayout or something?
|
Gary |
I mean stacked, so that the back button will work on that page. Here's a better example....
|
Gary |
Here's the base ViewPager [1] [2] [3] [4] [5]
|
Gary |
Here it is after adding fragment a to screen 2 [1] [2:a] [3] [4] [5]
|
Gary |
Here it is after adding fragment b to screen 2 [1] [2b] [3] [4] [5]
|
Gary |
make sense?
|
Mark M. |
and what does [2:a] and [2b] signify?
|
Mark M. |
in particular, I am confused by your use of the verb "add"
|
Mark M. |
are 2, a, and b all visible in the [2b] scenario?
|
Gary |
No, they aren't visible. I only want the top fragment to be visible.
|
Mark M. |
OK
|
Gary |
In the fragmentTransaction, I've tried both .add() and .replace()
|
Mark M. |
OK, let's back up a step
|
Mark M. |
are you using native fragments, or the backport from android-support-v4.jar?
|
Gary |
support library
|
Mark M. |
and the five pages (and hence your five bracketed numbers) are fragments?
|
Gary |
yes
|
Jul 29 | 7:35 PM |
Mark M. |
and what are you using for a PagerAdapter implementation? FragmentPagerAdapter? FragmentStatePagerAdapter? something you wrote to the PagerAdapter API yourself? something else?
|
Gary |
An extended FragmentPagerAdapter that sets up the initial state of [1] [2] [3] [4] [5]
|
Mark M. |
OK
|
Mark M. |
what you want will not work with that setup as it stands
|
Mark M. |
basically, FragmentPagerAdapter/FragmentStatePagerAdapter do not like others messing with their fragments
|
Mark M. |
what should work, at least in theory, is nested fragments
|
Gary |
That's kind of what I figured, since I've tried lots of things and nothing has worked.
|
Mark M. |
for page 2, rather than having your FragmentPagerAdapter return fragment 2, you'd return some sort of fragment that itself holds a fragment 2
|
Gary |
I've never used Nested Fragments. Is there a chapter in the book describing them?
|
Mark M. |
it's in there somewhere, I think :-)
|
Mark M. |
see "Hosting ViewPager in a Fragment"
|
Mark M. |
in the ViewPager chapter
|
Mark M. |
plus intro coverage of nested fragments in the fragments chapter in the "Fragments Within Fragments: Just Say “Maybe”" section
|
Mark M. |
to use your notation, your FragmentPagerAdapter would set up [1] [[2]] [3] [4] [5]
|
Jul 29 | 7:40 PM |
Mark M. |
and you would run your replace() FragmentTransactions against the child fragment manager of the outer fragment wrapped around [2]
|
Gary |
makes sense
|
Mark M. |
so you wind up with [1] [[2b]] [3] [4] [5] after two such replace() transactions
|
Mark M. |
now, whether the BACK button will work "out of the box" this way, I can't say
|
Mark M. |
on the whole, I'm not a huge fan of navigation within the page like this, so I haven't tried any of this
|
Gary |
Is there an alternative pattern that you'd recommend? This is a client project so I'm trying to implement what they've specified.
|
Mark M. |
either [2] should be launching a separate activity for a:b, or the whole thing shouldn't be in a ViewPager, IMHO
|
Mark M. |
the problem comes in with the BACK button
|
Mark M. |
suppose the user adds a and b
|
Mark M. |
now they switch to page 3
|
Mark M. |
now they press BACK
|
Mark M. |
what happens?
|
Mark M. |
and, do you feel confident that all users will agree that your answer makes sense? :-)
|
Gary |
good point
|
Jul 29 | 7:45 PM |
Gary |
Thanks for the help
|
Mark M. |
you are very welcome!
|
Gary |
I'll give these suggestions a try and if I get stuck, I'll be back on Thursday ;)
|
Mark M. |
OK, sounds good
|
Gary | has left the room |
Gary | has entered the room |
Mark M. |
hello again!
|
Gary |
Forgot I had one other unrelated question...
|
Gary |
I noticed in the latest version of the book you've "de-Sherlocked" everything.
|
Mark M. |
not everything yet
|
Mark M. |
the core chapters are still Sherlock-ful
|
Gary |
I have a lot of code that is sherlocked.
|
Mark M. |
though they too will be de-Sherlocked in version 6.0
|
Gary |
Wondering your thoughts if I should update legacy code, or just not use it for future projects?
|
Mark M. |
tough to say
|
Mark M. |
de-Sherlocking is not all that tough
|
Mark M. |
at the same time, continuing to use ActionBarSherlock is not a crime, either
|
Mark M. |
I am de-Sherlocking as part of a general push to move the book to be centered on a minSdkVersion of 15
|
Jul 29 | 7:50 PM |
Mark M. |
I'll move the Sherlock coverage to a chapter in the trails, next to the AppCompat chapter
|
anil | has entered the room |
anil |
hello
|
Gary |
OK, thanks again Mark.
|
Mark M. |
if you want to use that legacy code on a min 15 project, you can either keep using ActionBarSherlock, or you can de-Sherlock it
|
Mark M. |
Gary: no problem, happy to help
|
Mark M. |
anil: glad you were able to get in! It's your turn -- do you have a question?
|
anil |
thank you!
|
anil |
how I can get the system to start my service upon startup? On SO and in your book, you have a broadcast receiver listening to BOOT_COMPLETED. Is that the recommended way?
|
Mark M. |
it's pretty much the only way
|
Mark M. |
unless you're making a ROM mod or something
|
Mark M. |
it's certainly the most straightforward solution
|
Mark M. |
at least in most cases
|
anil |
ok
|
anil |
will do
|
Gary | has left the room |
anil |
2) In your book, the binder is a static, inner class. How can it access the outer methods in the Service class? In your BshBinder example, it does not.
|
Mark M. |
there is no outer methods of a static inner class
|
Mark M. |
a static inner class is syntactic sugar, identical to having a regular Java class
|
anil |
I meant, if I have a method in the Service.
|
Mark M. |
that doesn't change matters
|
Mark M. |
a static inner class of the service cannot access the service itself
|
Mark M. |
why is this code on the service and not on the binder?
|
Jul 29 | 7:55 PM |
anil |
is it kept static because it must be instantiated independent of the outside?
|
Mark M. |
no, it is static to help minimize the coupling between the components
|
Mark M. |
I'm not a huge fan of binding
|
Mark M. |
one of my worries is GC-related concerns
|
Mark M. |
it's entirely possible the Binder will work fine as a regular inner class
|
Mark M. |
in fact, I think I had it that way once
|
Mark M. |
I didn't need it to be a regular inner class, and so in a code cleanup pass a couple of years ago, I made it a static inner class
|
anil |
to your question, should I move the code from the service into the binder?
|
Mark M. |
IMHO, if you're using the binding pattern, more of your business logic probably winds up centered on the binder, not so much on the service itself
|
anil |
I thought a binder should not have functional application logic. I thought the service does the work.
|
Mark M. |
:: shrug :;
|
Mark M. |
that's not how I'd approach it, but I generally avoid binding anyway
|
anil |
why so?
|
Mark M. |
I prefer loose coupling, and binding is tight coupling
|
Mark M. |
I'd rather use startService() and an event bus
|
anil |
but its just an interface. corba etc depend upon interfaces
|
Mark M. |
it is state
|
Mark M. |
state that has to be managed across configuration changes
|
Mark M. |
admittedly, the retained fragment pattern for managing that state is a lot easier than pre-fragment solutions
|
Jul 29 | 8:00 PM |
anil |
I noticed the term in your book: configuration changes
|
Jul 29 | 8:00 PM |
Mark M. |
and for true IPC scenarios, there are going to be cases where binding is worth it
|
Jul 29 | 8:00 PM |
anil |
can you please clarify?
|
Mark M. | |
anil |
ok
|
Mark M. |
in the "Resource Sets and Configurations" chapter
|
Mark M. |
a configuration is a statement of device capabilities that, in Google's eyes, might cause us to want a particular set of resources
|
anil |
I will read it. but I cant think of state being maintained
|
Mark M. |
a configuration change is when the configuration changes on the fly
|
Mark M. |
such as when the screen rotates from portrait to landscape
|
Mark M. |
the ServiceConnection needs to be used to unbind
|
Mark M. |
and that is state
|
anil |
ok
|
Mark M. |
moreover, you cannot unbind the old activity instance and rebind in the new activity instance, as the service will be destroyed in between (if nobody else is bound to it)
|
Mark M. |
and so you need to pass the ServiceConnection, and the Binder, from the old activity instance to the new one
|
Mark M. |
again, a retained fragment makes this relatively easy
|
anil |
I did not realize that
|
anil |
so you recommend I use a started service?
|
Mark M. |
it's more that *I* would use startService() and an event bus for in-process use
|
Mark M. |
plenty of people use bound services
|
anil |
I have never used an event bus
|
Mark M. |
I added coverage of event buses to the book a couple of versions ago, and the tutorials now demonstrate the use of one
|
anil |
will have to read up on it. hope it isnt complex
|
Mark M. |
it's pretty easy, at least for simple scenarios
|
anil |
ok, will do.
|
anil |
thanks!
|
Mark M. |
you're very welcome
|
anil |
bye
|
Jul 29 | 8:05 PM |
anil | has left the room |
Jul 29 | 8:30 PM |
Mark M. | turned off guest access |