SetResult (int, intent) not working in Android
I have tried many solutions now. However, I cannot get the compiler to approve setResult (int, intent) in my activity. I got started with startActivityForResult.
Do you see the problem?
My code looks like this:
networkServiceMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//TODO: Implement the methods that should be executed on validation results
PrinterData printerData = intent.getParcelableExtra(NetworkService.VALIDATE_PRINTER_RESULT);
if(printerData != null)
{
Intent data = new Intent();
data.putExtra(VALIDATE_PRINTER_RESULT, printerData);
setResult(RESULT_OK, data);
finish();
}
else
{
}
}
};
Android Studio gives this error:
BroadcastReceiver
has its own setResult method that takes three parameters, not two, and you probably want to use setResult
from the class Activity
. Call it like:
ActivityName.this.setResult(RESULT_OK, data);
if BroadcastReceiver
declared insideActivity
RESULT_OK
is a constant of the Activity class.
In the Activity class you can access directly, but in other classes you also need to write the class name (Activity).
Use Activity.RESULT_OK
instead RESULT_OK
.