2

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.

Janneman96
  • 374
  • 1
  • 8
  • 24
  • 1
    The new Intent doesn't start the activity, there must be something like "startActivity" or "startActivityForResult" in your code .. could you please update your code sample? – Bmuig Jul 28 '16 at 16:38
  • Sorry, I forgot to add that line of code to the question. – Janneman96 Jul 28 '16 at 16:41
  • It's a switch with 7 cases which all put a different child class in the intent. After one has been chosen, it always does 4 inent.putExtra and activity.startActivityForResult(intent, requestCode). – Janneman96 Jul 28 '16 at 16:42

1 Answers1

5

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);
    }

}
solosodium
  • 723
  • 5
  • 15
  • Sorry, I didn't add that piece of code. I did call setResult and i'll add it to the question now. – Janneman96 Jul 28 '16 at 16:49
  • When I do not change the child activity, but call it from the actual parent activity instead of from the non-activity class which i called from the parent activity with .startQuiz(this, mModel), it does call the onActivityResult when it is finish() – Janneman96 Jul 28 '16 at 16:52
  • This is interesting... Could you add your onActivityResult code? Because it should work according to [this](http://stackoverflow.com/questions/2848775/use-startactivityforresult-from-non-activity). – solosodium Jul 28 '16 at 17:03
  • I've added the full onActivityResult now. Although it shouldn't be relevant, as the method isn't called at all. – Janneman96 Jul 28 '16 at 17:26
  • 1
    I tested myself with your implementation, it actually worked for me. I will post my version in this answer see if it answers your questions. – solosodium Jul 28 '16 at 18:57
  • I gave you an upvote for the effort :) thanks a lot for helping me! I tried moving the create statement of the Sample object to the button like you did and removed the 'final' keywords in the onActivityResult parameters. I tried putting 0 instead of my variable requestCode. I also tried using setIntent like you did instead of my setResult. I added the super.onActivityResult (even though the super method is empty) I tried it all in different combinations, but it doesn't work in my application :( – Janneman96 Jul 28 '16 at 19:50
  • @Janneman96 For the sake of understanding the issue, can you edit your question, and post detailed information such as: the Logcat of a sample program execution. The Device Model/Version as well as its manufacturer, A debug run, checking for breakpoints as well as the current stack. etc. Your issue seems to be that an "in between class" is receiving the result, doing nothing with it, then going back for Main. If it is so, changing `sample.startActivity(MainActivity.this);` to `sample.startActivity(v.getContext());` should work. – Bonatti Jul 29 '16 at 11:13
  • My colleague helped me on this one, it turns out i can't start an intent from a for each loop. I was looping through each question and showing an activity to ask the user for an answer. My colleague helped me out in creating another loop. I didn't know the loop information was relevant, thanks a lot for helping! – Janneman96 Jul 30 '16 at 09:49