Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 692
Devshan Avatar asked Sep 02 '14 03:09

Devshan


1 Answers

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
}
like image 96
Maxim Alov Avatar answered Nov 08 '22 04:11

Maxim Alov