"Compare Android and iOS development series"--activity and Uiviewcontroller

Source: Internet
Author: User

Contrast Development Series--activity and Uiviewcontroller

Activity and Uiviewcontroller

Android activity and fragment are the most basic interface components, while iOS is Uiviewcontroller. Almost all of the view and space is placed in activity and Uiviewcontroller.

There are a number of extensions on top:

Android:Fragmentactivity, appcompatactivity

IOS:Uitableviewcontroller, Uicollectionviewcontroller

Let's compare the inheritance relationship:

Android:Activity->ContextThemeWrapper->ContextWrapper->Context

IOS:UIViewController->UIResponder->NSObject

Almost all of the base classes in iOS are Nsobject,android and object, which is generally used as the base class for model layer objects.

Life cycle

This is a lot of information, I briefly say:

Android activity, onCreate() in the initialization of operations, onResume() you can add some changes in the interface and state operations;

iOS Uiviewcontroller, in the initialization of the -viewDidLoad operation, -viewWillAppear you can add some changes in the interface and state operations;

Compare:

Activity:OnCreate () –> OnStart () –> onresume () –> 运行态 –> onPause () –> onStop () –> OnDestroy ()

UIViewController:-viewdidload–>-viewwillappear–>-viewdidappear–> 运行态 –>-viewwilldisappear–>-viewDidDisappear

Here to add an Android

Fragment:* *onattach () –> onCreate () –> Oncreateview ( ) –> onactivitycreate () –> OnStart () –> onresume () –> 运行态 –> onPause () –> onStop () –> ondestroyview () –> OnDestroy () –> Ondetach ()

Both Android and iOS use the data structure of the stack to store activity and Uiviewcontroller.

AndroidOn the activity stack, you can search for taskAffinity and launchMode . All activity in the same application has the same affinity (taskaffinity), can be set by Itent flag, or in Androidmanifest.

IOSThe Uinavigationcontroller in the stack to Uiviewcontroller.

Interface jump and pass value

Android:Activity can use bundles with intent,fragment. For interface callback values, received via Startactivityforresult () Start and Onactivityresult ().

IOS:When initializing a Uiviewcontroller object, assign a value directly to the variable in the object. For interface callback values, you can customize the interface (Delegate), or you can use the notification (Notification)

struct Type class Code
//AndroidClass A extends B implements C
//IOSA.h@interface A : B A.m@implementation A <C>

Strong references and weak references

Android:

There are four kinds of reference types, strong references (strongreference), soft references (softreference), weak references (weakreference), virtual references (Phantom Reference).
Objects that are typically created are strong references. Therefore, when there is not enough memory space, the Java virtual Machine prefers to throw an oom exception, and it will not reclaim strongly referenced objects at random.
For soft references, enough memory space, the garbage collector will not recycle it, you can do the picture cache.

For weak applications, use scenarios for example: When using handler in Activity, it is necessary to define it as a static inner class form so that it is decoupled from the external class (Activity) and no longer holds references to external classes. At the same time, since Handlermessage in handler generally needs to access or modify the properties of the activity, it is necessary to define a weakreference within handler that points to this activity. So that it does not affect the activity's memory collection at the same time, the properties of the activity can be accessed under normal circumstances.

Ios:

With __weak, __strong is used to modify variables, declaring an object __strong by default.
In a strong reference, sometimes a circular reference occurs, and a weak reference is required to help (__weak).
A strong reference holds an object, and a weak reference does not hold an object.
A strong reference can free an object, but a weak reference cannot, because a weak reference does not hold an object, and when a weak reference points to an object held by a strong reference, the weak reference is automatically assigned nil when the strong reference is freed, i.e. the weak reference automatically points to nil.

Private and public

There are methods in iOS -``+ that are - equivalent to private in Android,
+Equivalent to public static in Android.

For global variables, iOS is placed in appdelegate or in. h using the # define declaration.
Android also, put in application or use public static in the class.
Of course, you can use a singleton class.

Basic controls

Compare some of the commonly used

Android IOS
TextView UILabel
TextEdit Uitextfield Uitextview
ImageView Uiimageview
Button UIButton
Switch Uiswitch
Listview TableView
Gridview CollectionView

Compare Inheritance:
Android views--View
IOS views, UIView, Uiresponder-NSObject

Java actually any object is directly or indirectly inherited from object, write extends object and do not write extends is equivalent.
So Android and iOS objects are essentially inherited from the top-level object. amazing~


Postscript
Write so much for the time being, and then add.
This is only the first of the series, and then write more.
The article and the Code, also need to continue to comb, constantly iterative.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Compare Android and iOS development series"--activity and Uiviewcontroller

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.