的onActivityResult不会调用startActivityForResult [英] onActivityResult isn't called for startActivityForResult

查看:302
本文介绍了的onActivityResult不会调用startActivityForResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项菜单MainActivity一个'设置'项目。

当我启动 SettingsActivity ,所有直到我点击工作正常保存
按钮,并尝试完成 SettingsActivity 。本次活动结束,但它似乎
关闭父活动也。我在Eclipse正在研究这个。 Eclipse的说
该事仍在运行,因为它可以让我单击停止
按钮。我有在 MainActivity A计时器线程运行,但我测试了这个没有那个线程
它仍然不回去的onActivityResult()

我开始SettingsActivity是这样的:

 公共静态最终诠释ACTIVITY_CREATE = 1;@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例R.id.settings:
            尝试{
                意向意图=新意图(这一点,SettingsActivity.class);
                startActivityForResult(意向,ACTIVITY_CREATE);
            }
            赶上(例外五){
                Log.e(TAG,e.getMessage());
                完();
            }
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}

我期待在SettingsActivity结束()来让我这个功能,但
它没有。我有一个断点,在此处设置,它从来没有得到这里:

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图意图){
    super.onActivityResult(要求code,结果code,意向);    开关(要求code){
        案例(ACTIVITY_CREATE):{
            如果(结果code == RESULT_OK){
            }
            打破;
       }
    }
}

下面是一个简单的SettingsActivity:

 公共类SettingsActivity扩展活动实现View.OnClickListener {
    私人按钮来保存;    @覆盖
        公共无效的onCreate(包B){
            super.onCreate(二);
            的setContentView(R.layout.settings);
            节省=(按钮)findViewById(R.id.save);
            save.setOnClickListener(本);
            返回;
        }    @覆盖
        公共无效的onClick(视图v){            开关(v.getId()){
                案例R.id.save:
                    意向意图=新的Intent();
                    intent.putExtra(IP,ipText.getText()的toString());
                    的setResult(RESULT_OK,意向);
                    完();
                    打破;                默认:
                    打破;
            }            返回;
        }
} //公共类SettingsActivity延伸活动{

有主要活动的launchMode设置为标准。

我的问题是,为什么不让我在调用活动回去的onActivityResult()?

谢谢,鲍勃

下面是清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.MyStuff
      安卓版code =1
      机器人:=的versionName1.0>
    <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:CTNETNAME =
            机器人:标签=@字符串/ APP_NAME
            机器人:screenOrientation =fullSensor
            机器人:configChanges =方向|屏幕尺寸| keyboardHidden
            机器人:launchMode =singleTask
        >
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>        < /活性GT;
        <活动
            机器人:SettingsActivityNAME =
        >        < /活性GT;    < /用途>    <采用-SDK安卓的minSdkVersion =8/> <! - targetSdkVersion后 - >    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>    <使用特征的android:glEsVersion =0x00020000机器人:所需=真/>< /清单>

想在这里补充详细的logcat:

  12月6日至30日:18:58.292:我/的System.out(16197):发送WAIT块
12月6日至30日:18:58.292:W / ActivityThread(16197):应用com.MyStuff正在等待调试器端口8100 ...
12月6日至30日:18:58.300:I / dalvikvm(16197):调试器是活跃
12月6日至30日:18:58.495:我/的System.out(16197):调试器连接
12月6日至30日:18:58.495:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:58.698:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:58.901:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:59.097:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:59.300:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:59.503:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:59.706:我/的System.out(16197):等待调试器来解决...
12月6日至30日:18:59.901:我/的System.out(16197):等待调试器来解决...
12月6日至30日:19:00.104:我/的System.out(16197):等待调试器来解决...
12月6日至30日:19:00.3​​07:我/的System.out(16197):等待调试器来解决...
12月6日至30日:19:00.511:我/的System.out(16197):调试先后落户(1476)
12月6日至30日:19:00.722:D / SOV(16197):MainActivity:在OnCreate
12月6日至30日:19:01.003:D / CTNET(16197):创建视图
12月6日至30日:19:01.003:D / CTNET(16197):视图中创建
12月6日至30日:19:01.065:我/的System.out(16197):CTNET:起点
12月6日至30日:19:01.128:我/的System.out(16197):BMA254加速度传感器
12月6日至30日:19:01.128:我/的System.out(16197):供应商=博世传感器
12月6日至30日:19:01.128:我/的System.out(16197):版本= 42602
12月6日至30日:19:01.128:我/的System.out(16197):最大射程= 19.613300
12月6日至30日:19:01.136:我/的System.out(16197):分延时= 10000
12月6日至30日:19:01.136:我/的System.out(16197):功率= 0.130000
12月6日至30日:19:01.136:我/的System.out(16197):分辨率= 0.038307
12月6日至30日:19:01.136:我/的System.out(16197):类型= 1
12月6日至30日:19:01.136:我/的System.out(16197):MS-3E(YAS530)磁传感器
12月6日至30日:19:01.136:我/的System.out(16197):供应商=雅马哈公司
12月6日至30日:19:01.143:我/的System.out(16197):版本= 42602
12月6日至30日:19:01.143:我/的System.out(16197):最大射程= 800.000000
12月6日至30日:19:01.143:我/的System.out(16197):分延时= 10000
12月6日至30日:19:01.143:我/的System.out(16197):功率= 4.000000
12月6日至30日:19:01.143:我/的System.out(16197):分辨率= 0.300000
12月6日至30日:19:01.143:我/的System.out(16197):类型= 2
12月6日至30日:19:01.151:我/的System.out(16197):MS-X方向传感器
12月6日至30日:19:01.151:我/的System.out(16197):供应商=雅马哈公司
12月6日至30日:19:01.151:我/的System.out(16197):版本= 42602
12月6日至30日:19:01.151:我/的System.out(16197):最大射程= 360.000000
12月6日至30日:19:01.151:我/的System.out(16197):分延时= 10000
12月6日至30日:19:01.151:我/的System.out(16197):功率= 0.000000
12月6日至30日:19:01.151:我/的System.out(16197):分辨率= 1.000000
12月6日至30日:19:01.151:我/的System.out(16197):类型= 3
12月6日至30日:19:01.151:我/的System.out(16197):AL3201光传感器
12月6日至30日:19:01.151:我/的System.out(16197):供应商= LITEON
12月6日至30日:19:01.151:我/的System.out(16197):版本= 42602
12月6日至30日:19:01.159:我/的System.out(16197):最大射程= 0.000000
12月6日至30日:19:01.159:我/的System.out(16197):最小延迟= 0
12月6日至30日:19:01.159:我/的System.out(16197):功率= 0.000000
12月6日至30日:19:01.159:我/的System.out(16197):分辨率= 0.000000
12月6日至30日:19:01.159:我/的System.out(16197):类型= 5
12月6日至30日:19:01.159:我/的System.out(16197):自动旋转传感器
12月6日至30日:19:01.159:我/的System.out(16197):供应商=三星电子
12月6日至30日:19:01.159:我/的System.out(16197):版本= 1
12月6日至30日:19:01.159:我/的System.out(16197):最大射程= 255.000000
12月6日至30日:19:01.159:我/的System.out(16197):最小延迟= 0
12月6日至30日:19:01.167:我/的System.out(16197):功率= 0.000000
12月6日至30日:19:01.167:我/的System.out(16197):分辨率= 0.000000
12月6日至30日:19:01.167:我/的System.out(16197):类型= 15
12月6日至30日:19:01.167:E /的SensorManager(16197):线程启动
12月6日至30日:19:01.167:D /的SensorManager(16197):registerListener ::手柄= 1 name = BMA254加速度传感器延迟= 200000
12月6日至30日:19:01.253:D / CTNET(16197):在onStart
12月6日至30日:19:01.261:D / CTNET(16197):onResume
12月6日至30日:19:01.487:D / SV(16197):surfaceCreated
12月6日至30日:19:01.487:D / SV(16197):surfaceChanged
12月6日至30日:19:08.190:W /编舞(16197):已经有一个未决的垂直同步事件。应该只有一次。
12月6日至30日:19:08.222:D / CTNET(16197)的onPause
12月6日至30日:19:08.245:D /的SensorManager(16197):unregisterListener ::
12月6日至30日:19:08.245:D /传感器(16197):保持听众= ..发送正常延时200毫秒
12月6日至30日:19:08.245:I /传感器(16197):sendDelay --- 2亿
12月6日至30日:19:08.245:D /的SensorManager(16197):JNI - sendDelay
12月6日至30日:19:08.245:我/的SensorManager(16197):设定正常延时=真
12月6日至30日:19:08.323:E / ViewRootImpl(16197):sendUserActionEvent()== MView的空
12月6日至30日:19:08.487:D /设置(16197):192.168.1.200
12月6日至30日:19:08.487:D /设置(16197):9072
12月6日至30日:19:08.979:D / SV(16197):surfaceDestroyed
12月6日至30日:19:09.089:D / CTNET(16197)的onStop
12月6日至30日:19:10.682:D /设置(16197):点击保存
12月6日至30日:19:10.729:W /编舞(16197):已经有一个未决的垂直同步事件。应该只有一次。
12月6日至30日:19:10.948:W / IInputConnectionWrapper(16197)上的非活动InputConnection showStatusIcon
12月6日至30日:19:11.104:D / SOV(16197):MainActivity ::的onDestroy


解决方案

我发现这个问题。我在MainActivity的的onStop()函数。

的结尾结束()

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

解决方案

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

这篇关于的onActivityResult不会调用startActivityForResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆