7

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

Devshan
  • 351
  • 1
  • 8
  • 19
  • 1
    is the calendar designed to return a value? if not it will always return a 0. – TomTsagk Sep 02 '14 at 03:20
  • I don't think Android (Google) Calendar will return any result, at least it's not specified in [Calendar Provider](http://developer.android.com/guide/topics/providers/calendar-provider.html). – Andrew T. Sep 02 '14 at 03:27
  • If it is so, Does Android calender provider provide any methods to know whether user click save the calender or Cancel? – Devshan Sep 02 '14 at 03:35
  • try to ignore the `resultCode` for a moment. Do you get any `Intent data` back? `if(data != null)` does it return `true`? can you get `data.getExtras()?` `bundle` out of it? print it to logcat, what do you have in it? – nightfixed Sep 09 '14 at 09:20
  • "data" always returns null. The following Log is displayed in logcat. " Can't update model with a Calendar cursor until it has seen an Event cursor." – Devshan Sep 09 '14 at 09:52

1 Answers1

4

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
}
Maxim Alov
  • 604
  • 4
  • 14