开始新的 activity 不会暂停当前的

Starting new activity does not pause current

在我的代码中我调用了一个新的 activity 但旧的没有暂停

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getX();
        float y = event.getY();
        float[] userCordinates = new float[2];

        userCordinates[0] = x;
        userCordinates[1] = y;
        userSequence.add(userCordinates);
        for (int r = 0; r < copySeq.size(); r++) {
            ImageView iv = (ImageView) (findViewById((Integer) copySeq.get(r)));
            int[] loc = new int[2];
            iv.getLocationOnScreen(loc);
            float xRangeMax = iv.getRight();
            float xRangeMin = iv.getLeft();
            float yRangeMax = iv.getBottom();
            float yRangeMin = iv.getTop();

            Integer point = (Integer)copySeq.get(r);

            if (x <= xRangeMax && x >= xRangeMin
                && y <= yRangeMax && y >= yRangeMin) { 
                if(copyColor.get(r).equals("green")){
                Intent intent = new Intent(this, ChildLevel.class);
    startActivity(intent);
            }
            break;
        }
    }
}

当新的 Activity 启动时,当前 Activity 中的这段代码片段被执行,但它应该在我回来时执行。例如。 Activity 应该恰好在此时暂停。

    if (userSequence.size() >= finalSequence.size()) {
        childLevel=false;
        save();
        check(userSequence);
        touchView.setEnabled(false);
    }
}    
return false;

谁能告诉我我做错了什么?谢谢!

当您启动 ChildLevel Activity 时,当前的 MainActivity 将暂停(调用 onPause() 方法)。

如果您希望在返回 MainActivity 时执行第二个代码片段,请将该代码放入 MainActivity

中的 onResume() 方法中

编辑:因此,您只需要在从 ChildLevel 返回到 MainActivity 时执行那段代码。您需要使用 startActivityForResult():

MainActivity 中,而不是 startActivity(),使用 startActivityForResult():

Intent intent = new Intent(this, ChildLevel.class);
startActivityForResult(i, 123);

然后,在ChildLevel中,当你想返回时:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();

最后,在 MainActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 123) {
        if (resultCode == Activity.RESULT_OK){
            // the code you want to execute
        }
    }
}