Janneman96
Janneman96

Reputation: 505

onActivityResult doesn't get called by finish() when the parent activity is passed as parameter to an intent in a non-activity class

I've got a class which handles a question sequence. It doesn't extend Activity. In the class there is the method:

public class QuizMaster {
    public void startQuiz(Activity activity, Model model) {
        //switch - case statement using model

        Intent intent = new Intent(activity, QuestionTextActivity.class)
        activity.startActivityForResult(intent, requestCode);

        //other case statements with other intents
    }
}

When I call this method from a working activity with

mQuizMaster.startQuiz(this, mModel);

And I finish() the child activity:

Intent returnIntent = new Intent();
returnIntent.putExtra(ARG_SELECTED_CHECKBOX, checkedBox);
setResult(RESULT_CODE, returnIntent);
finish();

it doesn't execute the parent activity's

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    Log.d(LOG_TAG, "OnActivityResult called in SignDetailsActivity. Resultcode is: ");
}

But when I execute the

Intent intent = new Intent(activity, QuestionTextActivity.class)
activity.startActivityForResult(intent, requestCode);

in the actual parent activity file, it does execute the onActivityResult method.

Why doesn't the child activity run the onActivityResult in the parent activity if sent with a non-activity class? How do i fix this?

I haven't found anyone with the same problem with executing new Intent() in a non-activity class like this. If there is someone, i didn't use the right search keywords and some others might type in the same as I did and come on this page.

Upvotes: 2

Views: 2588

Answers (1)

solosodium
solosodium

Reputation: 733

You need to call setResult(int) before call finish(). This is from Activity documentation:

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

Here is my implementation, which worked:

MainActivity.java (parent activity)

public class MainActivity extends AppCompatActivity {

    private Sample sample;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sample = new Sample();
                sample.startActivity(MainActivity.this);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("TEST", "DONE");
    }
}

LaunchActivity.java (child activity)

public class LaunchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setIntent(new Intent());
                finish();
            }
        });
    }
}

Sample.java (class start activity)

public class Sample {

    public Sample () {}

    public void startActivity (Activity a) {
        Intent it = new Intent(a, LaunchActivity.class);
        a.startActivityForResult(it, 0);
    }

}

Upvotes: 5

Related Questions