0

尝试将 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 打印出正确的日志,但在 onActivityResut 方法中没有打印该日志,这意味着它不会返回,我不知道这背后的原因。我移动了 switch 块来覆盖 finish() 方法,我遇到了同样的问题。

4

3 回答 3

0

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

于 2012-05-21T04:37:43.633 回答
0

线,

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

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

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

于 2012-05-20T23:47:31.583 回答
0

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

startActivityForResult(intent.setClass(this, CalendarQuickAddActivity.class), REQUEST_CAL_ACT);
于 2015-08-06T13:41:39.543 回答