2

I have MainActivity and WorkActivity in my app. WorkActivity starting by MainActivity:

Intent intent = new Intent(view.getContext(), WorkActivity.class);
intent.putExtra("intArray", intArray);
startActivity(intent);

In WorkActivity I read intArray param and start foreground service with notification:

protected void onCreate(Bundle savedInstanceState) {
    int[] intArray = getIntent().getIntArrayExtra("intArray");
    Intent intent = new Intent(this, MyService.class);
    intent.putExtra("intArray", intArray);
    startService(intent);
}

Service notification:

Intent newIntent = new Intent(this, WorkActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, newIntent, 0);
Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("title)
            .setContentIntent(pIntent)
            .setContentText("text");
Notification notification;
notification = builder.build();
startForeground(1, notification);

Now, when I close my app and click to notification occur error, because WorkActivity can't get

getIntent().getIntArrayExtra("intArray");

that should bу passed by MainActivity. How to fix this?

Log:

java.lang.RuntimeException: Unable to start service com.test.MyService@9051e18 with Intent { cmp=com.test/.MyService (has extras) }: java.lang.NullPointerException: Attempt to read from null array

Error occur because WorkActivity.getIntent().getIntArrayExtra("intArray") is null. Activity send this to service in lines:

intent.putExtra("intArray", intArray);
startService(intent);

In service I need to get some numbers from intArray like intArray[0], but intArray is null

tpmanc
  • 23
  • 4
  • Pls add your logcat to the question – Sebastian Walla Oct 22 '15 at 20:49
  • There's a wealth of information already on passing variables, objects through notifications: https://www.google.com/search?client=ubuntu&channel=fs&q=android+notifications+passing+variable&ie=utf-8&oe=utf-8 – CmosBattery Oct 22 '15 at 22:48

1 Answers1

1

When you go to the notification bar and select the notification, this starts a new instance of WorkActivity. This instance of WorkActivity is not started from your MainActivity, but by the notification, and the Intent you put in the notification does not have the extra intArray.

Basically you need to add the putExtra() call for intArray into this code:

    Intent newIntent = new Intent(this, WorkActivity.class);
    // HERE YOU NEED TO CALL newIntent.putExtra("intArray", intArray);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, newIntent, 0);
    Notification.Builder builder = new Notification.Builder(this)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle("title)
        .setContentIntent(pIntent)
        .setContentText("text");
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you so much. WorkActivity have line startService(intent); and when WorkActivity create after notificaton click, service started one more time. How to avoid this? – tpmanc Oct 24 '15 at 17:51
  • I don't completely understand your requirement, but if you want `WorkActivity` to know that it has been started from a notification (in case the behaviour should be different), you can add another "extra" to the `Intent` that you put in the notification. Then, `WorkActivity` can check this "extra" to determine if it has been started from a notification. – David Wasser Oct 24 '15 at 18:51
  • Ok, that's what I need. Thank you – tpmanc Oct 24 '15 at 19:17