Android uses reflection to solve version compatibility problems.

Source: Internet
Author: User

Each version update of Android has many new features that are expected, along with the addition of new classes or enhancement of some class functions in the API.
However, compatibility issues will be added. For example, after Android, you can directly call
Overridependingtransition (INT, INT) to implement the activity jump animation. But there is no such method in the API before 2.0.
Therefore, your program cannot run in versions earlier than Android. Considering compatibility issues, you may choose not to use the new method.

Is there a simple and compatible method? The answer is yes. It can be called using reflection. The Code is as follows:

Try {
// For Android> 2.0
Method methid = activity. Class. getmethod ("overridependingtransition", integer. type, integer. type );
Method. invok (youractivity, R. anim. zoom_in, R. anim. zoom_out );
} Catch (nosuchmethodexception e ){
// For Android <2.0
// Complete by other ways.
// You code...
} Catch (otherexception ex ){
Throw (Ex );
}

For new classes, some processing can also be done through reflection. Here we will not list them one by one. Readers can think about it and get their own solutions.

I haven't updated my blog for a long time. I feel that this question can be explained in a few words, so I can write it down and hope it will be helpful to some friends.

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.