简体   繁体   中英

Android Calendar: onActivityResult's resultCode is always 0

I develop an Android application that prompt the calendar application to edit events.

I use startActivityForResult() to open the Calender. After editing and saving the event, resultCode is always 0 inside onActivityResult() .

I saw many answers related to "onActivityResult resultCode always returns 0". This is because of not using the setResult() and finish() in the 2nd activity.

But in my case, I am calling the Android calendar application (not a custom activity).

The code to prompt the Android calendar:

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.

additionally I need to get the data back from the calendar intent.But The intent "data" in the onActivityResult also returns 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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