setResult没有回调用父活动

时间:2012-05-20 23:04:22

标签: android android-activity

尝试将setResult恢复为父活动时,会发生一些奇怪的事情 onActivityResult方法没有执行,我的应用程序终止 这是父母

@Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            Log.d("MyAPP","Access Calendar");
            //Getting the token back from authorization process
            accessToken = getIntent().getExtras().getString("token");
            // Setting up Calendar API Service
            setupCalendarConnection();
            startActivityForResult(intent.setClass(this, CalendarTasks.class),REQUEST_CAL_TASK);

    }
 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            Log.d("MyAPP", "result arrived");

            if(requestCode==REQUEST_CAL_TASK){
                switch (resultCode){
                    case CREATE_EVENT:
                        getUserCalendars();
                        break;
                    case QUICKADD_EVENT:
                        getUserCalendars();
                        startActivityForResult(intent.setClass(this, CalendarQuickAddActivity.class),REQUEST_CAL_ACT);
                        break;
                    case VIEW_EVENTS:
                        getUserCalendars();   
                        break;
                    case CREATE_CALENDAR:
                        getUserCalendars();
                        break;
                    case EDIT_CALENDAR:
                        getUserCalendars(); 
                        break;
                }
            }

            super.onActivityResult(requestCode, resultCode, data);
        }

这是儿童活动

listView.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                listIndex=position;
                Log.d("MyAPP", ""+position);

                switch (listIndex){
                    case 0:
                        Log.d("MyAPP","case 0"+listIndex);
                        setResult(CREATE_EVENT);
                        finish();
                        break;
                    case 1:{

                        Log.d("MyAPP","case 1"+listIndex);
                        setResult(QUICKADD_EVENT);
                        finish();
                        break;
                    }
                    case 2:
                        Log.d("MyAPP","case 2"+listIndex);
                        setResult(VIEW_EVENTS);
                        finish();
                        break;
                    case 3:
                        Log.d("MyAPP","case 3"+listIndex);
                        setResult(CREATE_CALENDAR);
                        finish();
                        break;
                    case 4:
                        Log.d("MyAPP","case 4"+listIndex);
                        setResult(EDIT_CALENDAR);  
                        finish();
                        break;
                }

            }

        });

检查案例1和Log cat打印出正确的Log但不打印登录onActivityResut方法,这意味着它不会返回,我不知道这背后的原因。 我移动开关块以覆盖finish()方法,我遇到了同样的问题。

3 个答案:

答案 0 :(得分:0)

这一行,

startActivityForResult(intent.setClass(this, CalendarQuickAddActivity.class), REQUEST_CAL_ACT);

确保您的结果返回CalendarQuickAddActivity。这是你想要的吗?

另外,变量intent究竟是什么?它是全球性的吗?这有点令人困惑......您可能只想使用Intent创建一个新的new Intent(this, CalendarQuickAddActivity.class),以使您的代码更具可读性。

答案 1 :(得分:0)

如果您正在使用ActivityGroup或TabGroupActivity,则需要覆盖handleActivityResult(requestCode,resultCode,data)而不是onActivityResult(requestCode,resultCode,data)。

答案 2 :(得分:0)

你需要调用startActivityForResult然后它才会返回

startActivityForResult(intent.setClass(this, CalendarQuickAddActivity.class), REQUEST_CAL_ACT);