I am creating something like Facebook Chat Heads, and I need to have access to FragmentManager in Service. Many people would say that I can't do that, but there always is a way, so I ask you guys, how can I access FragmentManager from Service?
In my Service, in onCreate
method I have:
LayoutInflater inflater = LayoutInflater.from(this);
layout = inflater.inflate(R.layout.box, null);
ViewPager pager = (ViewPager) layout.findViewById(R.id.pager);
FragmentManager fm = [HOW CAN I ACCESS THIS?];
pager.setAdapter(new MyPagerAdapter(fm));
I need to have access to FragmentManager in Service
Not only is that not possible, but you do not need it. There is nothing in Android that can only be accomplished via fragments.
For example, you appear to be using a ViewPager
. You do not need to use fragments with a ViewPager
. In fact, a ViewPager
itself has nothing to do with fragments. The two concrete PagerAdapter
implementations (FragmentPagerAdapter
and FragmentStatePagerAdapter
) happen to use fragments. But it is perfectly possible to create a PagerAdapter
implementation of your own that does not use fragments. And, it will take you substantially less time to implement a PagerAdapter
that works with Views
than it will to somehow cram a FragmentManager
-- tied inextricably to the activity lifecycle -- into a Service
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With