Android startActivityForResult和onActivityResult具有不可预测的行为

时间:2012-11-13 08:14:10

标签: java android

我有一个应用程序,其中多个活动按特定顺序显示,可以根据当前活动中的事件进行更改。我有一个“主”活动,管理显示哪些活动。每个动画都以startActivityForResult()开头,requestCode对该活动唯一。

当活动消失时,我将resultCode设置为一个对主活动有意义的值。在主活动的onActivityResult方法中,我有一个switch (requestCode),它会告诉我哪些活动已经返回,并且在每种情况下我都使用resultCode来确定下一个要启动的活动

我遇到的问题是,有时似乎是随机的,应用程序表现得非常不规律,不按顺序显示活动。

我在调试时无法复制问题,所以我所有的信息看起来都不错,但最终用户总是在抱怨不稳定的行为。

如何测试以查看问题所在?


主活动的代码onActivityResult:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case Settings.SCREEN_UPDATE:
        ShowActivity(LoginActivity.class, Settings.SCREEN_LOGIN);
        break;
    case Settings.SCREEN_LOGIN: 
        if (resultCode == RESULT_OK) {
            Settings.CurrentReport = new Report();
            Settings.CurrentReport.setUserId(data.getIntExtra("userId", -1));

            selectStore();
        } else {
            finish();
        }
        break;
    case Settings.SCREEN_PRODUCT: // Coming back from the product selection screen
        if (resultCode == RESULT_OK) {
            ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
        } else if (resultCode == RESULT_CANCELED){
            ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
        }
        break;
    case Settings.SCREEN_ACTION: // Coming back from the action screen, regardless of result, show options screen.
        ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
        break;
    case Settings.SCREEN_OPTIONS: // All choices return result_ok.  Check the "mode" extra
        String mode = data.getStringExtra("mode");
        processOption(mode);
        break;
    case Settings.SCREEN_SESSION:
        if (resultCode == RESULT_CANCELED) {
            ShowActivity(OptionsActivity.class, Settings.SCREEN_OPTIONS);
        } else if (resultCode == RESULT_OK ){
            ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
        }
        break;
    } // switch (requestCode)
} // protected void onActivityResult(int requestCode, int resultCode, Intent data) {

private void processOption(String mode) {
    if (mode.equals("select")) {
        ShowActivity(ProductActivity.class, Settings.SCREEN_PRODUCT);
    } else if (mode.equals("repeat")) {
        Settings.CurrentReport.repeatItem();
        ShowActivity(ActionActivity.class, Settings.SCREEN_ACTION);
    } else if (mode.equals("session")) {
        ShowActivity(SessionActivity.class, Settings.SCREEN_SESSION);
    } else { // mode equals "end"
        confirmFinish();
    }
}

private void ShowActivity(Class cls, int requestCode) {
    Intent activity = new Intent(this, cls);
    startActivityForResult(activity, requestCode);
}

然后,来自处理finish()事件的其他活动之一的代码:


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.options);

    Button btn;

    btn = (Button)findViewById(R.id.btnSelect);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = getIntent();
            intent.putExtra("mode", "select");
            setResult(RESULT_OK, intent);
            finish();
        }
    });

    btn = (Button)findViewById(R.id.btnRepeat);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = getIntent();
            intent.putExtra("mode", "repeat");
            setResult(RESULT_OK, intent);
            finish();
        }
    });
    btn.setEnabled(Settings.CurrentReport.hasPreviousItem());

    btn = (Button)findViewById(R.id.btnSession);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = getIntent();
            intent.putExtra("mode", "session");
            setResult(RESULT_OK, intent);
            finish();
        }
    });

    btn = (Button)findViewById(R.id.btnEnd);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = getIntent();
            intent.putExtra("mode", "end");
            setResult(RESULT_OK, intent);
            finish();
        }
    });

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            Intent i = getIntent();
            i.putExtra("mode", "end");
            setResult(RESULT_OK, i);
            finish();
            break;

    }
    return super.onKeyDown(keyCode, event);
}

1 个答案:

答案 0 :(得分:0)

我只是猜测,但也许Intent intent = getIntent();导致问题。您可以尝试将其替换为Intent intent = new Intent();

getIntent()返回启动当前活动的意图,不应将其用作结果。