我有一个带有选项菜单的MainActivity,其中包含'设置'项目
当我启动SettingsActivity
时,一切正常,直到我点击保存
按钮并尝试完成SettingsActivity
。此活动结束但出现
也关闭父活动。我正在Eclipse中研究这个问题。 Eclipse说
某物仍在运行,因为它允许我点击停止
按钮。我确实有一个在MainActivity
中运行的计时器线程,但是我在没有该线程的情况下测试了它
它仍然没有回到onActivityResult()
。
我以这种方式启动SettingsActivity:
public static final int ACTIVITY_CREATE = 1;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
try {
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
}
catch (Exception e) {
Log.e(TAG, e.getMessage());
finish();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我希望在SettingsActivity中使用finish()来获取此功能,但是 它没有。我在这里设置了一个断点,它永远不会到达这里:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case (ACTIVITY_CREATE): {
if (resultCode == RESULT_OK) {
}
break;
}
}
}
以下是简单的SettingsActivity:
public class SettingsActivity extends Activity implements View.OnClickListener {
private Button save;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.settings);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(this);
return;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
Intent intent = new Intent();
intent.putExtra("ip", ipText.getText().toString());
setResult(RESULT_OK, intent);
finish();
break;
default:
break;
}
return;
}
} // public class SettingsActivity extends Activity {
主要活动的launchMode设置为"标准"。
我的问题是为什么我不回到调用活动中的onActivityResult()?
谢谢,鲍勃
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyStuff"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity
android:name=".CTNet"
android:label="@string/app_name"
android:screenOrientation = "fullSensor"
android:configChanges = "orientation|screenSize|keyboardHidden"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" /> <!-- after targetSdkVersion -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
</manifest>
尝试在此处添加详细的logcat:
06-30 12:18:58.292: I/System.out(16197): Sending WAIT chunk
06-30 12:18:58.292: W/ActivityThread(16197): Application com.MyStuff is waiting for the debugger on port 8100...
06-30 12:18:58.300: I/dalvikvm(16197): Debugger is active
06-30 12:18:58.495: I/System.out(16197): Debugger has connected
06-30 12:18:58.495: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:58.698: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:58.901: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:59.097: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:59.300: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:59.503: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:59.706: I/System.out(16197): waiting for debugger to settle...
06-30 12:18:59.901: I/System.out(16197): waiting for debugger to settle...
06-30 12:19:00.104: I/System.out(16197): waiting for debugger to settle...
06-30 12:19:00.307: I/System.out(16197): waiting for debugger to settle...
06-30 12:19:00.511: I/System.out(16197): debugger has settled (1476)
06-30 12:19:00.722: D/SOV(16197): MainActivity::onCreate
06-30 12:19:01.003: D/CTNet(16197): creating view
06-30 12:19:01.003: D/CTNet(16197): view created
06-30 12:19:01.065: I/System.out(16197): CTNet: starting
06-30 12:19:01.128: I/System.out(16197): BMA254 Acceleration Sensor
06-30 12:19:01.128: I/System.out(16197): vendor = Bosch Sensortec
06-30 12:19:01.128: I/System.out(16197): version = 42602
06-30 12:19:01.128: I/System.out(16197): maximum range = 19.613300
06-30 12:19:01.136: I/System.out(16197): min delay = 10000
06-30 12:19:01.136: I/System.out(16197): power = 0.130000
06-30 12:19:01.136: I/System.out(16197): resolution = 0.038307
06-30 12:19:01.136: I/System.out(16197): type = 1
06-30 12:19:01.136: I/System.out(16197): MS-3E (YAS530) Magnetic Sensor
06-30 12:19:01.136: I/System.out(16197): vendor = Yamaha Corporation
06-30 12:19:01.143: I/System.out(16197): version = 42602
06-30 12:19:01.143: I/System.out(16197): maximum range = 800.000000
06-30 12:19:01.143: I/System.out(16197): min delay = 10000
06-30 12:19:01.143: I/System.out(16197): power = 4.000000
06-30 12:19:01.143: I/System.out(16197): resolution = 0.300000
06-30 12:19:01.143: I/System.out(16197): type = 2
06-30 12:19:01.151: I/System.out(16197): MS-x Orientation Sensor
06-30 12:19:01.151: I/System.out(16197): vendor = Yamaha Corporation
06-30 12:19:01.151: I/System.out(16197): version = 42602
06-30 12:19:01.151: I/System.out(16197): maximum range = 360.000000
06-30 12:19:01.151: I/System.out(16197): min delay = 10000
06-30 12:19:01.151: I/System.out(16197): power = 0.000000
06-30 12:19:01.151: I/System.out(16197): resolution = 1.000000
06-30 12:19:01.151: I/System.out(16197): type = 3
06-30 12:19:01.151: I/System.out(16197): AL3201 Light Sensor
06-30 12:19:01.151: I/System.out(16197): vendor = LITEON
06-30 12:19:01.151: I/System.out(16197): version = 42602
06-30 12:19:01.159: I/System.out(16197): maximum range = 0.000000
06-30 12:19:01.159: I/System.out(16197): min delay = 0
06-30 12:19:01.159: I/System.out(16197): power = 0.000000
06-30 12:19:01.159: I/System.out(16197): resolution = 0.000000
06-30 12:19:01.159: I/System.out(16197): type = 5
06-30 12:19:01.159: I/System.out(16197): Auto Rotation Sensor
06-30 12:19:01.159: I/System.out(16197): vendor = Samsung Electronics
06-30 12:19:01.159: I/System.out(16197): version = 1
06-30 12:19:01.159: I/System.out(16197): maximum range = 255.000000
06-30 12:19:01.159: I/System.out(16197): min delay = 0
06-30 12:19:01.167: I/System.out(16197): power = 0.000000
06-30 12:19:01.167: I/System.out(16197): resolution = 0.000000
06-30 12:19:01.167: I/System.out(16197): type = 15
06-30 12:19:01.167: E/SensorManager(16197): thread start
06-30 12:19:01.167: D/SensorManager(16197): registerListener :: handle = 1 name= BMA254 Acceleration Sensor delay= 200000
06-30 12:19:01.253: D/CTNet(16197): onStart
06-30 12:19:01.261: D/CTNet(16197): onResume
06-30 12:19:01.487: D/SV(16197): surfaceCreated
06-30 12:19:01.487: D/SV(16197): surfaceChanged
06-30 12:19:08.190: W/Choreographer(16197): Already have a pending vsync event. There should only be one at a time.
06-30 12:19:08.222: D/CTNet(16197): onPause
06-30 12:19:08.245: D/SensorManager(16197): unregisterListener::
06-30 12:19:08.245: D/Sensors(16197): Remain listener = Sending .. normal delay 200ms
06-30 12:19:08.245: I/Sensors(16197): sendDelay --- 200000000
06-30 12:19:08.245: D/SensorManager(16197): JNI - sendDelay
06-30 12:19:08.245: I/SensorManager(16197): Set normal delay = true
06-30 12:19:08.323: E/ViewRootImpl(16197): sendUserActionEvent() mView == null
06-30 12:19:08.487: D/settings(16197): 192.168.1.200
06-30 12:19:08.487: D/settings(16197): 9072
06-30 12:19:08.979: D/SV(16197): surfaceDestroyed
06-30 12:19:09.089: D/CTNet(16197): onStop
06-30 12:19:10.682: D/settings(16197): save clicked
06-30 12:19:10.729: W/Choreographer(16197): Already have a pending vsync event. There should only be one at a time.
06-30 12:19:10.948: W/IInputConnectionWrapper(16197): showStatusIcon on inactive InputConnection
06-30 12:19:11.104: D/SOV(16197): MainActivity::onDestroy
答案 0 :(得分:0)
我相信它是因为launchMode
,请在AndroidManifest
中查看
使用singleTop
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
[EDITED]
执行此更改并告诉我在您点按设置视图时会发生什么。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, ACTIVITY_CREATE);
break;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
尝试将mainActivity设置为
android:launchMode="singleTask"
在AndroidManifest中。
答案 2 :(得分:0)
尝试将方法从protected
更改为public
答案 3 :(得分:0)
我发现了问题。我在MainActivity的onStop()函数结尾处有一个finish()。