1

I've created a Dialog as an activity, where the user checks one of three radio buttons and the result is returned to the main activity. I followed the answer on this SO question

I thought that by creating this the MainAcitvity would pause and wait for a result from the child activity.

private void getFinalFinish() {

    Intent intent_openDialog = new Intent(this, DaAAmountToFinish.class);
    // Start the SecondActivity
    Bundle bundle_PassToDialog = new Bundle();

    bundle_PassToDialog.putInt("EXTRA_SCORE_TO_SUBTRACT", scoreToSubtractFrom);
    bundle_PassToDialog.putString("EXTRA_RADIO_BUTTON", rb_selected.getText().toString());
    bundle_PassToDialog.putString("EXTRA_THROWING", whosThrowing);        
    intent_openDialog.putExtras(bundle_PassToDialog);
    startActivityForResult(intent_openDialog, DIALOG_REQUEST_CODE);


}

This code is executed when wanted, I can see this as it is displayed whenever I hit the back button (My main activity continues to the point where it opens another activity that I only want to open after I get the result from activity as a dialog).

On activity result code...

 // This method is called when the dialog activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Check that it is the SecondActivity with an OK result
    if (requestCode == DIALOG_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {


            Bundle extras = data.getExtras();
            // Get String data from Intent
            int darts_to_minus = extras.getInt("DARTS_TO_MINUS");
            boolean addTooScore = extras.getBoolean("ADD_TO_SCORE");
            int dartsToSubtractFromFinish = extras.getInt("DARTS_TO_SUBTRACT_FROM_FINISH");

            setMatchAVG(darts_to_minus, whosThrowing, addTooScore);


            if (newBestLeg) {

                if (whosThrowing.equalsIgnoreCase("Player 1")) {
                    tv_bestLeg.setText(getString(R.string.tv_BestLeg, String.valueOf(tempNumOfDartsThrownP1 + dartsToSubtractFromFinish))); //Subtract 2 off best leg only took 1 dart to finish
                } else {
                    tv_bestLegP2.setText(getString(R.string.tv_BestLeg, String.valueOf(tempNumOfDartsThrownP2 + dartsToSubtractFromFinish)));
                }
            }
        }
    }
}

Why is this?

COYG
  • 1,538
  • 1
  • 16
  • 31
  • please add the relevant code where you call getFinalFinish() so we can see what else is in the same scope. – Nikos Hidalgo Feb 13 '19 at 17:18
  • it's just called from an if statement which is in a method with 200+ lines. Even then that method is called from another. – COYG Feb 13 '19 at 17:48
  • I have no onPause method in this Activity, would this matter? – COYG Feb 13 '19 at 17:55

1 Answers1

2

My main activity continues to the point where it opens another activity that I only want to open after I get the result from activity as a dialog

This is "working as intended":

  • once a method has started, it will continue until it is finished (if it doesn't take too long and so causes an ANR).

  • The default thread for execution of Activity methods is the main thread. Specifically, if another Activity is started, its methods will be executed on the main thread as well.

  • So the first method from the new Activity can only be executed once the method which started the new Activity has finished.

  • it follows that you should move all the parts which have to wait for the result from the new Activity to another part of your app. You override onActivityResult() and trigger the execution of these parts based on the result.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61