我有一款适用于Android 2.x的Android应用,但是使用Ice Cream Sandwhich失败了。
App具有Mainactivity A,当单击“Setup”按钮时,使用onStartActivityForResult()启动SetupActivity B.
通过SetupActivity,用户可以启动其他活动来设置应用程序设置(网络连接设置,用户界面设置等)。 SetupActivity使用onStartActivityForResult()启动其中的一些,并将有关已更改设置的数据放入使用setResult返回的Intent(RESULT_OK,intent);
当SetupActivity启动了另一个活动C(即连接设置)并且C返回时,MainActivity A再次被创建了无意义的量(即50次)(或者多次调用它的onCreate())。可能是什么原因以及如何继续解决这个问题?这个问题只发生在Ice Cream Sandwhich上。
MainActivity:
//OnClick listeners for all the buttons and text field in the UI
public void onClick(View view) {
//start SetupActivity
if (view == (setup_button)){
Intent intent = new Intent(getApplicationContext(), SetupActivity.class);
startActivityForResult(intent, SETUP_ACTIVITY);
}
...
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
switch(requestCode) {
case SETUP_ACTIVITY: //returned from SetupActivity
//check what settings have changed and update variables accordingly
...
}
}
SetupActivity:
//click handler for "Measurement setup" button
public void measSetupClicked(View v) {
Intent intent = new Intent(this, MeasurementSetupActivity.class);
startActivityForResult(intent, MEAS_SETUP);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent intent = new Intent();
//try-catch omited for brevity
if(data.hasExtra("MEAS_SETTINGS_CHANGED")) {
intent.putExtra("MEAS_SETUP_CHANGED"),data.getExtras().getBoolean("MEAS_SETTINGS_CHANGED"));
}
setResult(RESULT_OK,intent);
}
@Override
public void onBackPressed() {
SetupActivity.this.finish();
}
public void backButtonPressed(View v) {
SetupActivity.this.finish();
}