Pergunta

I am using a pending intent to launch an alarm clock (using AlarmManager). I need different result code of the launched activity based on which one of the 2 buttons placed in it the user clicks (Snooze or Cancel). How do I get this result? Unfortunately, the method onActivityResult() is not launched in the parent activity after finish() method is launched on the closing activity. In the Android documentation, it states

"PendingIntent.getActivity: Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)"

I would need something like Context.startActivityForResult(Intent) but for Pending Intent.

Foi útil?

Solução

PendingIntents are designed that they can be launched from other applications, i.e. it isn't clear who should receive the result. That's why startActivityForResult() is meaningless for PendingIntent. My suggestion is to send a broadcast message when you finish the previously launched activity

@Override
public void onFinish() {
    super.onFinish();
    Intent intent = new Intent(YOUR_CUSTOM_ACTION);
    // Put data to intent
    sendBroadcast(intent);
}

And receive the result in other activity using BroadcastReceiver:

public class ActivityResultReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // Retrieve data from intent
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top