[Comparison Between Android and IOS development series]: Activity and UIViewController and uiviewcontroller

Source: Internet
Author: User

[Comparison Between Android and IOS development series]: Activity and UIViewController and uiviewcontroller
Comparison between development series Activity and UIViewController

Activity and UIViewController

Android Activity and Fragment are the most basic interfaces, while IOS is the UIViewController. Almost all views and spaces are stored in Activity and UIViewController.

There are many extensions above:

Android:FragmentActivity, AppCompatActivity

IOS:UITableViewController, UICollectionViewController

Let's compare the inheritance relationship:

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

IOS: UIViewController->UIResponder->NSObject

Almost all base classes in IOS are NSObject, and objects in Android are also used as the base classes of Model-layer objects.

Lifecycle

There are a lot of materials in this regard. Let me simply say:

Android Activity,onCreate()Initialization,onResume()You can add some operations to change the interface and status;

IOS UIViewController,-viewDidLoadInitialization,-viewWillAppearYou can add some operations to change the interface and status;

Comparison:

Activity:OnCreate ()-> onStart ()-> onResume ()->Running State-> OnPause ()-> onStop ()-> onDestroy ()

UIViewController:-ViewDidLoad->-viewWillAppear->-viewDidAppear->Running State->-ViewWillDisappear->-viewDidDisappear

Here is an Android

Fragment:** OnAttach ()-> OnCreate ()->OnCreateView ()->OnActivityCreate ()-> OnStart ()-> onResume ()->Running State-> OnPause ()-> onStop ()->OnDestroyView ()-> OnDestroy ()->OnDetach ()

Android and IOSStackActivity and UIViewController.

AndroidYou can search for the stack of an Activity.taskAffinityAndlaunchMode. All the activities of the same application have the same affinity (taskAffinity), which can be set through Itent FLAG or in AndroidManifest.

IOSUINavigationController in uses the stack to UIViewController.

Page Jump and value passing

Android:The Activity can use Intent, and the Fragment can use Bundle. For interface callback value passing, start with startActivityForResult () and receive onActivityResult.

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

Structure Type Code
//AndroidA.java Class A extends B implements C
//IOSA.h @interface A : B A.m @implementation A <C>
Strong reference and weak reference

Android:

There are four Reference types: StrongReference, SoftReference, WeakReference, and Phantom Reference ).
Generally, the created objects are strongly referenced. Therefore, when the memory space is insufficient, the Java Virtual Machine would rather throw an OOM exception and not recycle strongly referenced objects at will.
For soft references, the memory space is sufficient, and the garbage collector will not recycle it, so it can cache images.

For weak applications, for example, when Handler is used in an Activity, it must be defined as a static internal class form to decouple it from an external class, do not hold external class references. At the same time, because handlerMessage in Handler generally needs to access or modify the attributes of the Activity, you need to define the WeakReference pointing to the Activity in Handler, so that it does not affect the memory collection of the Activity, and the Activity attributes can be accessed under normal circumstances.

IOS:

Use _ weak and _ strong to modify variables. By default, an object _ strong is declared.
In the case of strong references, loop references may occur sometimes. In this case, weak references are needed to help (_ weak ).
Strong references hold objects, and weak references do not hold objects.
Strong references can release objects, but weak references cannot, because weak references do not hold objects. When a weak reference points to an object held by a strong reference, when the strong reference releases the object, weak references are automatically assigned nil, that is, weak references automatically point to nil.

Private and public

In IOS- +Method,-Equivalent to private in Android,
+It is equivalent to public static in Android.

For global variables, IOS is placed in AppDelegate or declared in. h using # define.
Android also uses public static in Application or class.
Of course, you can use the singleton class.

Basic controls

Compare some common

Android IOS
TextView UILabel
TextEdit UITextField UITextView
ImageView UIImageView
Button UIButton
Switch UISwitch
ListView TableView
GridView CollectionView

Comparison of inheritance:
Android Views-> View
IOS Views> UIView> UIResponder> NSObject

In Java, all objects are directly or indirectly inherited from objects. Writing extends objects is equivalent to not writing extends objects.
Therefore, Android and IOS objects are inherited from top-level objects in essence.Amazing ~

Postscript
Write so much for the time being, and then add more.
This is only the first article in this series, and I will write more later.
Like the code, the article also needs to be constantly sorted and iterated.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.