Aleksandr
Aleksandr

Reputation: 1465

Get an object "FragmentManager" in "Service"

Is it possible to get an object "FragmentManager" in "Service"? Is it possible to pass an object "FragmentManager" from "Activity" in the "Service".

PS: Includes not officially supported features.

Upvotes: 3

Views: 3363

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

Is it possible to get an object "FragmentManager" in "Service"?

No, sorry.

Is it possible to pass an object "FragmentManager" from "Activity" in the "Service".

That would be an exceptionally bad idea.

If you wish for your service to update your UI, bear in mind that there might not be a UI. The user is welcome to press their BACK or HOME button to exit your UI, even while your service is running.

Also bear in mind that it might be a different UI. For example, when the user rotates the device from portrait to landscape, your activity (and fragments, by default) will be destroyed and recreated. Or, the user may tap on something that brings a different activity to the foreground, and that activity has its own fragments and manager.

Therefore, to have your service update your UI, you need to use a communications path that supports this sort of decoupled operation, such as:

  • send a broadcast
  • send a broadcast using LocalBroadcastManager
  • update content via a ContentProvider, with activities using CursorLoader or ContentObserver to be notified about data changes
  • etc.

Upvotes: 9

Related Questions