简体   繁体   English

StartActivityForResult不暂停主要活动

[英]StartActivityForResult not pausing Main Activity

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. 我认为通过创建此MainAcitvity将暂停并等待子活动的结果。

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? 为什么是这样?

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). 一旦方法开始,它将继续进行直到完成为止(如果方法用时不长,因此会导致ANR)。

  • The default thread for execution of Activity methods is the main thread. 执行Activity方法的默认线程是主线程。 Specifically, if another Activity is started, its methods will be executed on the main thread as well. 具体来说,如果启动另一个Activity ,则其方法也将在主线程上执行。

  • So the first method from the new Activity can only be executed once the method which started the new Activity has finished. 因此,只有在启动新Activity的方法完成后才能执行新Activity的第一个方法。

  • 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. 接下来,您应该将所有需要等待结果的所有部分从新Activity移到应用程序的另一部分。 You override onActivityResult() and trigger the execution of these parts based on the result. 您重写onActivityResult()并根据结果触发这些部分的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM