0

我有一个应用程序,其中多个活动按特定顺序显示,可以根据当前活动中的事件进行更改。我有一个“主”活动来管理显示哪些活动。每个活动都以该活动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);
}

然后,处理完成()事件的其他活动之一的代码:


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

1 回答 1

0

我只是猜测,但可能Intent intent = getIntent();是造成问题的原因。您可以尝试将其替换为Intent intent = new Intent();

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

于 2012-11-13T14:01:28.693 回答