App Portal "Ios-android Development vs."

Source: Internet
Author: User

App Portal "Ios-android Development vs."

[Picture Android vs IOS]

Outline

    1. Compare and analyze the entrance of Ios,android,

    2. Ios,android Interface Unit

    3. Why do we have that life cycle?

    4. How inheritance and abstract classes are written, such as Factory mode

    5. The strength of the object, the features of iOS

Program entry (Entry point)



#首先来看iOS应用的入口:

int main(intchar * argv[]){    @autoreleasepool {        returnclass]));    } }

Like all C programs, the main function is the entrance to the OBJECTIVE-C program. Although this main method returns an int, it does not actually return. It will persist in memory until the user or system forces it to terminate.

Above UIApplicationMain it comes from UIKit , is a very important function.

Say the parameters, the first two parameters we all understand.

The third argument is the UIApplication class name or its subclass name, and if it is nil, the UIApplication class name is used by default.

The fourth parameter is the Appdelegate class as the delegate object for the application, which listens to the application lifecycle-related delegate methods.

The UIApplication central role of this is to provide control and collaboration during the operation of the iOS program. It creates several core objects of the app, such as: Uiapplicationdelegate UIWindow, UIView, to handle the process:

    1. The program entry main function creates the UIApplication instance and the UIApplication proxy instance.

    2. Loading the user interface from available storyboard files

    3. Call Appdelegate custom code to do some initialization settings

    4. Put the app in the main Run loop environment to respond to and handle events generated by user interaction

This UIApplication object is set at startup and Main Run Loop uses it to handle events and update the view-based interface, where main Run loop is the main thread of the application.

[Image IOS, Swift, Android tag]



Talk about Swift's entrance:

In the swift language, the compiler will no longer look for the main function as the entry for the program, but rather a main.swift file.
The first line of code in the file is implicitly considered the entry of the program, and you can add the following code:

UIApplicationMain(C_ARGC, C_ARGV, nil,     NSStringFromClass(AppDelegate))

Yes, that's what we mentioned earlier UIApplicationMain . Here C_ARGC, the C_ARGV global variable is the one in the main function.
ARGC, argv.

In addition, you can add tags to the swift file to indicate the entry of the @UIApplicationMain project. Doing so causes the compiler to ignore the Main.swift portal file, and the file labeled with the @uiapplicationmain label as a portal file.



#再来看看Android的:

Android Program you can't find an explicit main method

Although Java also has the main method, Android seems to be unable to find the main.

There are many explanations for this problem.

StackOverflow explained that there is no main because it does not need main, the system generates activity and calls its methods, the application default boot has replaced main, so there is no need to use the main method.

So where is the entrance to the program? We look at it from the Application beginning.

In each Android program package, there is a manifest file that declares its components, and we can see the following code:

<manifest  ...    <application  ...        <activity android:name=".MainActivity" android:label="@string/app_name">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application></manifest>

In this XML-written manifest file, the <application/> label is at the outermost layer. Among them, this marked the Android.intent.category.LAUNCHER <activity/> is the program start the default interface. But they are not the real entrances.

The real portal for Android apps is the ActivityThread.main method

This is an implicit entry, and the code has to be simplified as follows:

public  static  void  main  (string[] args) { Some detection defaults ...  Looper.preparemainlooper (); //create message loop looper  Activitythread thread = new  activitythread (); Thread.attach (false ); if  (Smainthreadhandler = = null ) {Smainthreadhandler = Thread.gethandler (); //UI thread Handler } Asynctask.init (); Looper.loop (); //execution message loop  throw  new  RuntimeException ( "Main thread loop unexpectedly exited" ); } 

#深入一下:

Inheritance Relationship:

java.lang.Object   ?    android.content.Context       ?    android.content.ContextWrapper           ?    android.app.Application

The bottom of Android is Linux Kernel , iOS is XNU Kernel , what's the difference between them?

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.

iis|

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~

About the stack principle of inheritance and abstract class app startup How to write the startup program entry for the Factory mode app

The article and the Code, also need to continue to comb, constantly iterative.

Reference

Ios

http://www.jianshu.com/p/aa50e5350852

Http://www.cnblogs.com/ydhliphonedev/archive/2012/07/30/2615801.html

http://swifter.tips/uiapplicationmain/

http://blog.ibireme.com/2015/05/18/runloop/

http://swifter.tips/uiapplicationmain/

Android

Http://www.cnblogs.com/lwbqqyumidi/p/4151833.html

Https://sites.google.com/site/terrylai14/home/android-context-yuan-li

http://blog.csdn.net/chenzheng_java/article/details/6215986

http://blog.csdn.net/chenzheng_java/article/details/6216621

http://blog.csdn.net/bboyfeiyu/article/details/38555547

Other

Android Program Entrance Application onCreate () What has been done, here is a crooked nut print out the onCreate after the stack display log:

Mainactivity.oncreate (Bundle) Line: AInstrumentation.callactivityoncreate (Activity, Bundle) Line:1047Activitythread.performlaunchactivity (Activitythread$activityrecord, Intent) Line:2627Activitythread.handlelaunchactivity (Activitythread$activityrecord, Intent) Line:2679activitythread.access$2300(Activitythread, Activitythread$activityrecord, Intent) Line: theActivitythread$h.handlemessage (Message) Line:2033Activitythread$h (Handler). DispatchMessage (Message) Line: AboutLooper.loop () Line:123Activitythread.main (string[]) Line:4627Method.invokenative (Object, object[], class, class[], class, int, Boolean) Line: notAvailable [native Method] Method.invoke (object, Object ...) Line:521Zygoteinit$methodandargscaller.run () Line:868Zygoteinit.main (string[]) Line:626Nativestart.main (string[]) Line: notAvailable [native Method]

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

App Portal "Ios-android Development vs."

Related Article

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.