1

I am using the following code to open the calendar app:

class Appointments : AppCompatActivity() {

    lateinit var tv:TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_appointments)
        tv = findViewById(R.id.textView4)

        tv.setOnClickListener(View.OnClickListener {
            var callIntent = Intent(Intent.ACTION_EDIT)
                    .setType("vnd.android.cursor.item/event")
            startActivityForResult(callIntent, 3);
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 3 && resultCode == Activity.RESULT_OK && data!=null){
            Toast.makeText(this@Appointments,"Some data came",Toast.LENGTH_SHORT).show()
        } else{
            Toast.makeText(this@Appointments,"Some Error",Toast.LENGTH_SHORT).show()
        }
    }
}

I keep getting 'some error' message. I tried removing 'data!=null' but I think the resultcode is the problem.

What I 'finally' want to achieve is this:

  • User opens the app
  • User clicks on a button to open the calendar app
  • User is able to see the calendar and then the user makes an appointment in the calendar
  • User comes back to the app and I am able to extract the date and time of the new appointment

Is it possible to do? If yes then some code example will be much appreciated. If it is not possible then what are the other ways to achieve this?

Damandroid
  • 756
  • 9
  • 31

1 Answers1

0

Remove the call to super. Calling super will change the requestCode. This fact isn't exactly clear in a lot of documentation but I have spun my tires on it in the past. Similarly, you will find this answer Wrong requestCode in onActivityResult useful if you encounter a similar issue with fragment to activity communication

Murph_Fish
  • 279
  • 2
  • 17
  • Thanks but I am not using a Fragment, so I am not sure if this is the issue. I did tried your suggestion but there is no difference : RequestCode: 3, ResultCode: 0, Expected: -1 – Damandroid Aug 30 '18 at 20:29
  • I see what is happening your resultCode is coming back as 0 but it expects -1 which is Activity.RESULT_OK you are getting back Activity.RESULT_CANCELED. You can see this in the ACTIVITY class ` /** Standard activity result: operation canceled. */ RESULT_CANCELED = 0; RESULT_OK = -1; RESULT_FIRST_USER = 1;` not sure why though. – Murph_Fish Aug 30 '18 at 21:24