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: enter image description here

+3


source to share


2 answers


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

+10


source


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

.

+1


source







All Articles