Office Hours Transcript: 2021-09-02

Kumar joined

Hi Mark

hello, Kumar!

 

how can I help you today?

Having a weird problem with viewpager.
https://stackoverflow.com/q/69017721/4732846

when you create the levelTwoViewPager’s adapter, what FragmentManager are you using?

 

it should be the getChildFragmentManager() of the fragment for the tab in the levelOneViewPager that is setting up levelTwoViewPager

I am using custom pager adapter extended from PagerAdapter for levelTwoViewPager

 

it should be the getChildFragmentManager() of the fragment for the tab in the levelOneViewPager that is setting up levelTwoViewPager
–> Yes, I am using childFragmentManger for levelTwoViewPager

 

The problem is when the fragment in viewPagerOne is recreated before it sets adapter, levelTwoPager is created with fragments with old data that I had set.

when you switch levelOneViewPager from page 1 to page 4, in your custom PagerAdapter for levelTwoViewPager, are you receiving calls to destroyItem()?

Haven’t checked for this.

 

Checking in a minute.

 

Build is taking time. Meanwhile on another note.

And also I am planning to read one of your android books to learn about best practices and structuring code in nice way.
Both Elements of Android jetpack and Exploring Android, seems similar, which one should I go with.

Exploring Android is for "learning by doing", while Elements of Android Jetpack is for "learning by reading"

 

Elements of Android Jetpack covers more material, as it is easier to explain concepts than it is to write instructions for you employ concepts

 

but, if you feel that your ability to learn is improved by doing the work yourself, Exploring Android is designed for that

Okay. Both covers same topic and in same depth?

 

Elements of Android Jetpack covers more topics and in greater depth

Okay.

going back to your original problem, it has been years since I dealt with nested ViewPager instances

Okay, I am checking for that callback.
I was not sure of where the error is , tried many of SO answers nothing worked.

since this is your own custom PagerAdapter, though, you are the one creating those fragments, so you should be able to get a fairly good sense of whether you are creating new fragments when you return to page 1 or whether your original fragments are still around and are being reattached

Just checked onDestroyItem is not called.

OK, so that further suggests that your fragments are still around

since this is your own custom PagerAdapter, though, you are the one creating those fragments, so you should be able to get a fairly good sense of whether you are creating new fragments when you return to page 1 or whether your original fragments are still around and are being reattached

–> Okay, I personally did not create that, had to resue the code from the existing project from a fellow mate. May be have to get a understanding from the colleague

 

OK, so that further suggests that your fragments are still around
–> Yes.

yeah, sorry, "you" in that sentence should be interpreted as meaning your team and not Google

Ha ha, I got your point though.

to be honest, "accessing some data in the parent fragment" is a bit of a code smell in modern Android app development

 

if you are using Jetpack’s ViewModel, you might look to have the fragments in your levelTwoViewPager share a ViewModel with the fragment that hosts that pager

Actually it is not the data, for simpler understanding put it as data, it is actually a dynamic view that gets created in the parent fragment

then that goes from "a bit of a code smell" to "a serious code smell", and I would more strongly steer you towards a shared ViewModel if you are using Jetpack’s viewmodel system

Yes we have viewmodel system.

then I would try to find ways to use that to better decouple the fragments in the levelTwoViewPager from the hosting fragment of that pager

Okay, I get the point, parent and child fragment are tightly coupled. Will use this viewModel to communicate. Ya I also that shared view model set up as well.

 

have*

so, for example, the parent fragment can publish data through the shared ViewModel via LiveData or StateFlow that the levelTwoViewPager fragments react to

Okay.

then, "not available yet" should no longer be a problem, as the reactive APIs help smooth over timing issues

Understood.

beyond that, it is difficult to provide advice in a chat for complex interactions like this

Okay. Ya it makes sense. This is how ideally it should be. And also solves this problem.

 

Thanks Mark. I don’t have any more questions.
As usually it got me to into right thinking, thanks for that.

I’m happy to help!

usual*

 

Bye Mark. Thanks for the help.
Good day.

 

And I believe transcript are still being saved even with this new chat system

yes, this will get posted to https://commonsware.com/office-hours/ a bit after the chat ends

 

(assuming that I don’t screw it up)

Lol. :)
I will take a break.

Kumar left