javaandroidandroid-intentbundles

Unable to retrieve data of type Paint.Cap from a bundle


I have looked everywhere for this and I couldn't find any answer.

I am sending data over an intent like this.

MainActivity.java

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("key",Paint.Cap.SQUARE);
            startActivity(intent);

My issue is in retrieving it, I don't know which get method to use and I couldn't find any source online that tells me.

SecondActivity.java

Bundle extras = getIntent().getExtras();

    if(extras !=null)
    {
        if(extras.containsKey("key"))
        {
            Paint.Cap shape = //which method to use here?

        }
    }

If it's something so silly please tell me, I am still a beginner and tried my best to find it on my own.

Thanks.


Solution

  • The key is that Paint.Cap is an enum and so it implements the interface Serializable.

    This means that you called the version of putExtra that takes a Serializable.

    To get it out again you can do

    Paint.Cap shape = (Paint.Cap) extras.getSerializable("key");