android/codelab-slices-basic-codelab

Question: why increase `sReqCode` for each call to `getChangeTempIntent` ?

AndroidDeveloperLB opened this issue · 1 comments

This code:

private PendingIntent getChangeTempIntent(int value) {
    Intent intent = new Intent(ACTION_CHANGE_TEMP);
    intent.setClass(context, MyBroadcastReceiver.class);
    intent.putExtra(EXTRA_TEMP_VALUE, value);
    return PendingIntent.getBroadcast(getContext(), sReqCode++, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
}

I think it's not really needed to increase it all the time, as the request might interfere with other kinds of codes.

I think it's easier to split it to inc&dec. For example:

private PendingIntent getChangeTempIntent(int value, boolean hasIncreased) {
    Intent intent = new Intent(ACTION_CHANGE_TEMP);
    intent.setClass(context, MyBroadcastReceiver.class);
    intent.putExtra(EXTRA_TEMP_VALUE, value);
    return PendingIntent.getBroadcast(getContext(), hasIncreased ? 0 : 1, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
}

And the usage:

    SliceAction tempUp = new SliceAction(getChangeTempIntent(sTemperature + 1, true),
            IconCompat.createWithResource(context, R.drawable.ic_temp_up).toIcon(),
            "Increase temperature");
    SliceAction tempDown = new SliceAction(getChangeTempIntent(sTemperature - 1, false),
            IconCompat.createWithResource(context, R.drawable.ic_temp_down).toIcon(),
            "Decrease temperature");

Seems to work fine...

I rewrote the codelab in Kotlin, and it is a bit different now, so I am closing this issue. Please reopen if you see the same issue and I will look into it ASAP.