onActivityResult isn't called for startActivityForResult
Asked Answered
S

4

0

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?

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
Snell answered 30/6, 2014 at 14:36 Comment(16)
the code should work the way you posted it here. I guess that the problem is somewhere else where you are not looking (and we are not seeing)Cosmology
where are you starting the activityForResult? inside an activity, or inside a fragment?Runck
is "onDestory" called for the calling activity ?Runck
@LenaBru protected void onActivityResult(int requestCode, int resultCode, Intent intent) method signature is from ActivityCp
@Cp your observation is incorrect, onActivityResult(int,int,Intent) is also the signature when received inside a fragment.Runck
add some Log.d before super.onActivityResult(...)Avulsion
@LenaBru it's public in Fragmentand protected in Activity.Cp
I put a breakpoint in MainActivity's onDestroy() and it IS being called. But I don't know why.Snell
add Log.d in onCreate, onActivityResult and onDestroy. what then do you see?Avulsion
Can you post the logcat error here ?Monadism
Maybe this is a big clue. There is one error message in logcat: 06-30 11:32:48.847: E/ViewRootImpl(9898): sendUserActionEvent() mView == nullSnell
so what is the logcat?Avulsion
06-30 11:32:48.847: E/ViewRootImpl(9898): sendUserActionEvent() mView == nullSnell
ok, seems you don't want to get any answer...Avulsion
In verbose mode the logcat is too long by 4700 characters to post here.Snell
I was able to add the verbose logcat to the original question. I've been looking into the mView==null error. It may have something to do with Samsung. I'm running this on a Samsung Galaxy Tab 2 (7 inch).Snell
C
0

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);
}
Cp answered 30/6, 2014 at 14:42 Comment(2)
I tried the return super.onOptionsItemSelected(item) idea and that made no difference.Snell
I see, your code looks pretty simple. Could you send both class code to try run them here?Cp
P
0

Try to set your mainActivity as

        android:launchMode="singleTask"

in your AndroidManifest.

Prerogative answered 30/6, 2014 at 14:49 Comment(1)
Still behaves the same.Snell
R
0

Try changing the method from protected to public

Runck answered 30/6, 2014 at 14:56 Comment(0)
S
0

I found the problem. I had a finish() at the end of MainActivity's onStop() function.

Snell answered 30/6, 2014 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.