Virendra
Virendra

Reputation: 21

How to call fragment from IntentService class?

I have a class named 'GeofenceIntentService' to receive Push Notification and I want to call fragment from that class, the class is extends IntentService class.

Thanks in advance.

Upvotes: 0

Views: 251

Answers (2)

Ilya
Ilya

Reputation: 116

  1. If you need to open a screen, you should open an Activity which contains your Fragment, but not a Fragment instead.

    For example:

    Intent i = new Intent(); i.setClass(GeofenceIntentService.this, HostActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i);

  2. If you need to communicate between Service and Fragment, look at Bound Service .

  3. If you need to send a message to Fragment from the Service, use BroadcastReceiver

Upvotes: 1

Sohit Sharma
Sohit Sharma

Reputation: 1

I solved the problem. if we are using this in Fragment its not find Activity context.

We are using MyFragment.this.getActivity()

Intent intent = new Intent(MyFragment.this.getActivity(),MyService.class);
intent.putExtra("path",uri);
MyFragment.this.getActivity().startService(intent);

Upvotes: 0

Related Questions