Android Calendar: onActivityResult resultCode always 0

I am developing an Android application that requests a calendar application for editing events.

I use startActivityForResult() to open a calendar. After editing and saving the event, resultCode always inside onActivityResult() .

I saw a lot of answers related to "onActivityResult resultCode always returns 0". This is because the second action does not use setResult() and finish() .

But in my case, I call the Android calendar application (not a custom action).

Code for the request in 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 start or save the calendar

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

I click "Save" or "Cancel" in the calendar application, resultCode always gives 0.

In addition, I need to return data from the intent of the calendar. But the intent β€œdata” in onActivityResult also returns null.

Can anyone explain why this is happening? Is there a way to find out if the user clicks "Save" or "Cancel"?

+6
source share
1 answer

You can check the lastId of a newly added calendar event, if it has not changed, then the result is really CANCELED, 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 } 
0
source

Source: https://habr.com/ru/post/974668/


All Articles