在我的活动中,我尝试在方法(如下所示)中使用validation
来执行两个功能:
以上情况正在运行,但在执行验证后,该活动中的program flow
仍在执行中。 (见下面的第二个代码块)。
如果在我的方法中调用验证块,如何确保活动中没有其他内容执行?即只返回MainMenu,不要在finally
块中执行其余代码。
包含验证的方法:
public void getAverageAttentionValue() {
// validation to ensure that no dividing by zero
if (totalofAttLevels < 1) {
new Thread() {
public void run() {
StroopGame.this.runOnUiThread(new Runnable() {
public void run() {
//display error message
Toast.makeText(
StroopGame.this,
"Headset unable to read values, please re-connect",
Toast.LENGTH_LONG).show();
(new Handler()).postDelayed(new Runnable() {
public void run() {
Intent openActivity = new Intent(
"com.example.brianapp.MainMenu");
startActivity(openActivity);
device.close();
}
}, 2000);
}
});
}
}.start();
} else {
averageAttLevel = totalofAttLevels / attCount;
attMax = Collections.max(AttentionValues);
}
}
调用finally块中的上述方法以及之后仍在调用的代码:
finally {
//calling method containing validation
getAverageAttentionValue();
//I DONT WANT THE CODE BELOW TO BE EXECUTED IF
//THE VALIDATION IN METHOD IS CALLED
// write data from session to SQLite db
writeToDatabase();
// stop audio
tenSecs.stop();
// declaring activity to open
Intent openActivity = new Intent(
"com.example.brianapp.StroopResults");
openActivity.putExtra("singleScore", single);
// Start activity
startActivity(openActivity);
device.close();
}
}
答案 0 :(得分:0)
你可以这样做:
在您的活动中获取成员变量,例如boolean isValidateCalled = false;
。如果调用了true
方法,则将该变量设置为validate()
。然后在finally
finally {
//calling method containing validation
getAverageAttentionValue();
// Check whether validate was called,if not,proceed
if(!isValidateCalled)
{
// write data from session to SQLite db
writeToDatabase();
// stop audio
tenSecs.stop();
// declaring activity to open
Intent openActivity = new Intent(
"com.example.brianapp.StroopResults");
openActivity.putExtra("singleScore", single);
// Start activity
startActivity(openActivity);
}
device.close();
}