我的应用程序出现了一个奇怪的问题,在OnPause()
和OnCreate()
之后立即调用我的活动OnResume()
。我有一些函数要在OnPause()
中执行,但我不知道为什么会这样。在我的日志中显示为:
05-14 13:40:09.916: E/TestActivity(840): onCreate()
05-14 13:40:09.936: E/TestActivity(840): onResume()
**05-14 13:40:09.956: E/TestActivity(840): onPause()**
05-14 13:40:09.956: E/TestActivity(840): onResume()
编辑
public class PreTestActivity extends Activity{
public static final String PREFS_NAME = "iPTPrefsFile";
private static final String TAG = "PreTestActivity";
SharedPreferences settings;
static final int HEART_RATE_DIALOG_ID = 0;
static final int BLOOD_PRESSURE_DIALOG_ID = 1;
static final int RESPIRATORY_RATE_ID = 2;
static final int O2_SATURATION_DIALOG_ID = 3;
Button testActionNextButton,testActionBackButton;
EditText Heart_Rate_EditText,Blood_Pressure_EditText,Respiratory_Rate_EditText,O2_Saturation_EditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pre_test);
Log.e(TAG, "onCreate()=");
settings = getSharedPreferences(PREFS_NAME, 0);
Heart_Rate_EditText=(EditText)findViewById(R.id.Heart_Rate_EditText);
Blood_Pressure_EditText=(EditText)findViewById(R.id.Blood_Pressure_EditText);
Respiratory_Rate_EditText=(EditText)findViewById(R.id.Respiratory_Rate_EditText);
O2_Saturation_EditText=(EditText)findViewById(R.id.O2_Saturation_EditText);
testActionNextButton = (Button)findViewById(R.id.testActionNextButton);
testActionNextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!Heart_Rate_EditText.getText().toString().equalsIgnoreCase("")&&!Blood_Pressure_EditText.getText().toString().equalsIgnoreCase("")
&&!Respiratory_Rate_EditText.getText().toString().equalsIgnoreCase("")&&!O2_Saturation_EditText.getText().toString().equalsIgnoreCase("")){
Log.d(TAG,"Heart_Rate_EditText="+Heart_Rate_EditText.getText().toString());
Log.d(TAG,"Blood_Pressure_EditText="+Blood_Pressure_EditText.getText().toString());
Log.d(TAG,"Respiratory_Rate_EditText="+Respiratory_Rate_EditText.getText().toString());
Log.d(TAG,"O2_Saturation_EditText="+O2_Saturation_EditText.getText().toString());
SharedPreferences.Editor editor = settings.edit();
float PreHeartRate=Float.parseFloat(Heart_Rate_EditText.getText().toString());
float PreBloodPressure=Float.parseFloat(Blood_Pressure_EditText.getText().toString());
float PreRepositoryRate=Float.parseFloat(Respiratory_Rate_EditText.getText().toString());
float PreO2saturation=Float.parseFloat(O2_Saturation_EditText.getText().toString());
editor.putFloat("preHeartRate", PreHeartRate);
editor.putFloat("preBloodPressure", PreBloodPressure);
editor.putFloat("preRepositoryRate", PreRepositoryRate);
editor.putFloat("preO2saturation", PreO2saturation);
editor.commit();
// TODO Auto-generated method stub
}
if(validateForm()) {
Intent PreTestActivityIntent = new Intent(getParent(), TimerActivity.class);
BergBalanceScaleActivity parentActivity = (BergBalanceScaleActivity)getParent();
parentActivity.replaceContentView("PreTestActivity", PreTestActivityIntent);
}
}
});
testActionBackButton=(Button)findViewById(R.id.testActionBackButton);
testActionBackButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//finish();
}
});
}
@Override
protected void onPause() {
super.onPause();
//isRecord=1;
Log.e(TAG, "onPause()=");
}
@Override
protected void onStop() {
super.onStop();
//isRecord=1;
Log.e(TAG, "onStop()=");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume()=");
if(settings.getFloat("preHeartRate", 0)==0.0 && settings.getFloat("preBloodPressure", 0)==0.0 && settings.getFloat("preRepositoryRate", 0)==0.0 && settings.getFloat("preO2saturation", 0)==0.0){
Heart_Rate_EditText.setText("");
Blood_Pressure_EditText.setText("");
Respiratory_Rate_EditText.setText("");
O2_Saturation_EditText.setText("");
}else{
//if(isRecord==1){
Heart_Rate_EditText.setText(""+settings.getFloat("preHeartRate", 0));
Blood_Pressure_EditText.setText(""+settings.getFloat("preBloodPressure", 0));
Respiratory_Rate_EditText.setText(""+settings.getFloat("preRepositoryRate", 0));
O2_Saturation_EditText.setText(""+settings.getFloat("preO2saturation", 0));
//}
}
}
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
AlertDialog dialog;
switch(id) {
case HEART_RATE_DIALOG_ID:
builder.setMessage("Pleas, enter Heart Rate.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
break;
case BLOOD_PRESSURE_DIALOG_ID:
builder.setMessage("Please enter Blood Pressure.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
break;
case RESPIRATORY_RATE_ID:
builder.setMessage("Please enter Respiratory rate.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
break;
case O2_SATURATION_DIALOG_ID:
builder.setMessage("Please enter O2 Saturation.")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
boolean validateForm() {
boolean result = true;
if("".equals(Heart_Rate_EditText.getText().toString())) {
showDialog(HEART_RATE_DIALOG_ID);
result = false;
} else if("".equals(Blood_Pressure_EditText.getText().toString())) {
showDialog(BLOOD_PRESSURE_DIALOG_ID);
result = false;
} else if("".equals(Respiratory_Rate_EditText.getText().toString())) {
showDialog(RESPIRATORY_RATE_ID);
result = false;
} else if("".equals(O2_Saturation_EditText.getText().toString())) {
showDialog(O2_SATURATION_DIALOG_ID);
result = false;
}
return result;
}
}
任何人都可以帮助我。