简体   繁体   English

Android日历:onActivityResult的resultCode始终为0

[英]Android Calendar: onActivityResult's resultCode is always 0

I develop an Android application that prompt the calendar application to edit events. 我开发了一个Android应用程序,它提示日历应用程序编辑事件。

I use startActivityForResult() to open the Calender. 我使用startActivityForResult()打开日历。 After editing and saving the event, resultCode is always 0 inside onActivityResult() . 编辑并保存事件后, resultCode onActivityResult() resultCode始终为0。

I saw many answers related to "onActivityResult resultCode always returns 0". 我看到许多与“onActivityResult resultCode总是返回0”相关的答案。 This is because of not using the setResult() and finish() in the 2nd activity. 这是因为在第二个活动中没有使用setResult()finish()

But in my case, I am calling the Android calendar application (not a custom activity). 但就我而言,我正在调用Android日历应用程序(不是自定义活动)。

The code to prompt the Android calendar: 提示Android日历的代码:

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
//set the event details
startActivityForResult(intent,1);

To trigger when calender is saved or cancelled 在保存或取消日历时触发

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //resultCode always returns 0.
    switch(requestCode) {
    case 1: 
        if (resultCode == Activity.RESULT_OK) 
        {

        }
    }
}

Whether I click "Save" or "Cancel" in the calendar app, the resultCode always gives 0. 无论我在日历应用程序中单击“保存”还是“取消”, resultCode始终为0。

additionally I need to get the data back from the calendar intent.But The intent "data" in the onActivityResult also returns null. 另外我需要从日历intent中获取数据。但是onActivityResult中的intent“data”也返回null。

Could anyone explain why it happens? 有谁可以解释为什么会发生? Is there any way to know if user clicks "Save" or "Cancel"? 有没有办法知道用户是否点击“保存”或“取消”?

You can check for lastId of newly added calendar event, if it has not changed, then result is actually CANCELLED, otherwise it is OK 您可以检查新添加的日历事件的lastId,如果没有更改,则结果实际上是CANCELED,否则就可以了

val projection = arrayOf(CalendarContract.Calendars._ID)
cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, projection, null, null, null)
if (cursor.moveToLast()) {
    val lastId = cursor.getLong(0)
    // compare lastId with a previous one, if not changed - result is CANCELLED
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM