What to note when the enum type is being carried by intent

Source: Internet
Author: User

In general, we use parcelable when we pass objects between activity. such as writing a class, on this class marked implements Parcelable and implementation interface can be used Intent.putextra (String, parcelable). For passing enum objects, it is assumed that this method is also used, that is, like http://stackoverflow.com/questions/2836256/ Passing-enum-or-object-through-an-intent-the-best-solution inside the 3 floor said:

[Java]View Plaincopy
  1. Public enum MyEnum implements parcelable {
  2. VALUE;
  3. @Override
  4. public int describecontents () {
  5. return 0;
  6. }
  7. @Override
  8. public void Writetoparcel (final Parcel dest, final int flags) {
  9. Dest.writeint (ordinal ());
  10. }
  11. public static final creator<myenum> Creator = new creator<myenum> () {
  12. @Override
  13. Public MyEnum Createfromparcel (final Parcel source) {
  14. return Myenum.values () [Source.readint ()];
  15. }
  16. @Override
  17. Public myenum[] NewArray (final int size) {
  18. return new myenum[size];
  19. }
  20. };
  21. }
  22. You can than use Intent.putextra (String, parcelable).


So we first define a myenum variable A, and then call Intent.putextra ("name", a), the method PutExtra (String, parcelable) is ambiguous for the type Intent's fault, why?

Since the enum itself implements the serializable interface, the Enum class is written in the source code:

[Java]View Plaincopy
    1. Public abstract class Enum<e extends Enum<e>> implements Serializable, comparable<e> {  
    2. ... ...
    3. }


Then your MyEnum class implements the Parcelable interface, and Intent has such two functions: Intent.putextra (String, parcelable) and Intent.putextra (string, Serializable), your MyEnum class implements Serializable and Parcelable two interfaces, and when you call Intent.putextra, the compiler does not know which Intent.putextra to select (String, parcelable) or Intent.putextra (String, Serializable), resulting in two semantics.  So is the above statement really useless? In fact, this kind of writing in the MyEnum object as a member of the class Father1 can still write, we in the activity between Father1,father1 can achieve parcelable interface. Father1 internal processing of MyEnum members can be as follows:

[Java]View Plaincopy
    1. Private Father1 (Parcel in) {
    2. Mfield = In.readint ();
    3. Mmyenum = MyEnum.CREATOR.createFromParcel (in);
    4. }
    5. Public void Writetoparcel (Parcel dest, int flags) {
    6. Dest.writeint (Mfield);
    7. Mmyenum.writetoparcel (dest, flags);
    8. }

The enum can also be passed out after testing.

Since the above myenum can be adapted to the enum as a member of the Parcelable class, and cannot be passed as a separate object, how should both be done?

First, MyEnum does not need to implement the Parcelable interface, the MyEnum object is passed by Intent.putextra (String, Serializable) alone.

The code for the MyEnum Createfromparcel () and Writetoparcel () is then integrated into the Father class, with the code snippet (variable name changed):

[Java]View Plaincopy
  1. Private Father2 (Parcel in) {
  2. Mfield = In.readint ();
  3. Manotherenum = Anotherenum.values () [In.readint ()];
  4. }
  5. Public static final parcelable.creator<father2> Creator = new parcelable.creator<father2> () { /c3>
  6. Public Father2 Createfromparcel (Parcel in) {
  7. return new Father2 (in);
  8. }
  9. @Override
  10. Public father2[] NewArray (int size) {
  11. return new father2[size];
  12. }
  13. };
  14. @Override
  15. Public void Writetoparcel (Parcel dest, int flags) {
  16. Dest.writeint (Mfield);
  17. Dest.writeint (Manotherenum.ordinal ());
  18. }

Then use Intent.putextra (String, parcelable) to pass the Father2 object.

Let's talk about the values () method of the Enum class, which is not visible through eclipse, it's defined here, it returns all the defined enumeration values, and the underlying implementation of the enum is to define a number of integers from 0 to N, but to take an alias for each integer. An enum variable is one of these integers, and the ordinal () method of the enum variable returns its position in the number of integers. The values () static method returns an array containing the number of integers.

To show the sample code (no point download), in the Mainactivity.java three points of comment, respectively, the interpretation of the comments and then run, you will know more clearly.

What to note when the enum type is being carried by intent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.