Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access FragmentManager in Service?

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));
like image 525
Damian Avatar asked Jun 03 '14 22:06

Damian


1 Answers

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.

like image 53
CommonsWare Avatar answered Oct 18 '22 02:10

CommonsWare