21

Can anyone please say how to launch a new Activity using PendingIntent and also to pass a value using pending intent. Thanks in advance.

Dmitrii Leonov
  • 1,331
  • 1
  • 15
  • 25
Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50

1 Answers1

33
Intent intent = new Intent(getApplicationContext(), ActivityToLaunch.class);
intent.putExtra(<oneOfThePutExtraFunctions>);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

You can add extra data into an Intent by using one of the various Intent.PutExtra() functions located: http://developer.android.com/reference/android/content/Intent.html

Then, when you are ready to launch your PendingIntent, use one of the Send() functions located: http://developer.android.com/reference/android/app/PendingIntent.html

Godfrey Duke
  • 1,505
  • 16
  • 19
sparksalot
  • 518
  • 4
  • 7
  • 3
    The documentation of `PendingIntent.getActivity` says "Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent". So you probably should do `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`. – user905686 Mar 04 '19 at 10:57
  • 1
    Starting 31, S, you need to specify PendingIntent.FLAG_IMMUTABLE like PendingIntent pi = PendingIntent.getActivity(requireActivity(), 0, intent, PendingIntent.FLAG_IMMUTABLE); – Smeet Aug 25 '22 at 07:50