问题
I have a MainActivity with an options menu with a 'settings' item.
When I start the SettingsActivity
, all works fine until I click the save
button and try to finish the SettingsActivity
. This activity ends but it appears
to close the parent activity also. I'm working on this in Eclipse. Eclipse says
that the something is still running because it allows me to click the stop
button. I do have a timer thread running in MainActivity
, but I tested this without that thread
and it still doesn't go back to onActivityResult()
.
I'm starting SettingsActivity this way:
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);
}
}
I'm expecting the finish() in SettingsActivity to get me to this function, but it doesn't. I have a breakpoint set here and it never gets here:
@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;
}
}
}
Here is the simple 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 {
The launchMode for Main Activity is set to "standard".
My question is why don't I get back to onActivityResult() in the calling activity?
Thanks, Bob
Here is the manifest file:
<?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>
Trying to add verbose logcat here:
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
回答1:
Try to set your mainActivity as
android:launchMode="singleTask"
in your AndroidManifest.
回答2:
Try changing the method from protected
to public
回答3:
I believe it's because the launchMode
, review it in AndroidManifest
Try it with singleTop
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
[EDITED]
Do this change and tell me what happen when you tap in settings view.
@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);
}
回答4:
I found the problem. I had a finish() at the end of MainActivity's onStop() function.
来源:https://stackoverflow.com/questions/24492644/onactivityresult-isnt-called-for-startactivityforresult