iOS VS Android

1. android系统使用一个Activity栈来管理Activity,而iOS使用UINavigationController进行管理

2. android启动一个新的Activity类似于iOS上创建一个UIViewController

When an app is launched, the OS pushes the app’s main activity onto the stack. Note that you can launch other apps’ activities and have them placed onto the stack. By default, the back button on Android pops from the OS activity stack, so when a user presses back, he or she can go through multiple apps that have been launched.


Android和iOS一个最大的不同,在Android中任何一个activity都可以作为app的入口,前提是它在AndroidManifest注册了。在AndroidManifest注册相当于告诉OS,这个activity可以作为程序入口。As a side note, model objects must implement the Parcelable interface if you want to send them between activities and fragments. Implementing the Parcelable interface is similar to conforming to the  protocol on iOS. Also note that Parcelable objects are able to be stored in an activity’s or fragment’s savedInstanceState, in order to more easily restore their states after they have been destroyed.

3. Application 方面,两者都是单例对象。

Android(Application类)----------->iOS(UIApplication类的代理UIApplicationDelegate)

onCreate----------->didFinishLaunchingWithOptions :程序成功启动后调用,而且只会调用一次

onStart-------------->applicationWillEnterForeground:程序进入前台

onResume---------->applicationDidBecomeActive:程序获取到焦点,可以接收触摸点击等

onPause------------->applicationWillResignActive:程序失去焦点,不可以接收触摸点击等

onStop--------------->applicationDidEnterBackground:程序进入后台

onDesroy----------->applicationWillTerminate:这里android已经销毁了,但iOS如果还需要在后头运行,就会触发这个方法

4. UIViewController 生命周期和 Activity生命周期对比

Android(Activity)------------->iOS(UIViewController)

onCreate----------------------->1.alloc:创建对象,分配空间

onCreate----------------------->2.init(initWithNibName) 初始化对象,初始化数据

onCreate----------------------->3.loadView:从nib载入视图 ,通常这一步不需要去干涉,除非你没有使用xib文件创建视图.


onResume---------------------->1.viewDidLoad: 载入完成,可以进行自定义数据以及动态创建其他控件

onResume---------------------->2.viewWillAppear:视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了

onResume---------------------->3.viewDidAppear:视图已在屏幕上渲染完成


onPause---------------------->viewWillDisappear:视图将被从屏幕上移除之前执行 

onStop----------------------->viewDidDisappear:视图已经被从屏幕上移除,用户看不到这个视图了 

onDesroy-------------------->dealloc :视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放

viewDidUnload :在发生内存警告的时候如果本视图不是当前屏幕上正在显示的视图的话, viewDidUnload将会被执行,本视图的所有子视图将被销毁,以释放内存,此时开发者需要手动对viewLoad、viewDidLoad中创建的对象释放内存。 因为当这个视图再次显示在屏幕上的时候,viewLoad、viewDidLoad 再次被调用,以便再次构造视图。


iOS VS Android_第1张图片
Activity生命周期

Activity的生命周期类似于UIViewController的生命周期,不同的是,Android OS可以随意的根据系统内存杀死Activity,所以,要确保保存了activity的状态和数据,以便恢复。最好的保存方式是用bundle从savedInstanceState或者Intents中恢复。

旋转屏幕时,生命周期会重置view,如果数据和状态保存完好了,可以实现无缝切换。

5. 配置文件

android 的manifest.xml 配置文件和iOS的Info.plist配置文件

AndroidManifest.xml file. This file is the equivalent of the Project-Info.plist file on iOS, and it stores information for activities, application names, and set Intents

 (system-level events) that the application can handle.

两者都是可以定义程序名称等参数的地方,不同的是,当改动Info.plist的Bundle name值去改变程序名后,必须点xcode的product-clean一下

修改其他值也一样,资源文件修改后必须clean一下才生效。

其他参数比如:程序版本号等,也在这里定义,不同的是ios程序的唯一标识不是包名而是identifler

6. Null check! Objective-C gracefully handles message sending to nil objects, but Java does not.

7. Project Structure

Android applications are primarily broken into two sections, the first of which is the Java source code. The source code is structured via the Java package hierarchy, and it can be structured as you please. However, a common practice is to use top-level categories for activities, fragments, views, adapters, and data (models and managers).

The second major section is the res folder, short for ‘resource’ folder. The resfolder is a collection of images, XML layout files, and XML value files that make up the bulk of the non-code assets. On iOS, images are either @2x or not, but on Android there are a number of screen density folders to consider.

 Android uses folders to arrange images, strings, and other values for screen density. The resfolder also contains XML layout files that can be thought of as xib files. Lastly, there are other XML files that store resources for string, integer, and style resources.

8.Fragment 生命周期


iOS VS Android_第2张图片
Fragment 生命周期

比较难处理的问题是fragments 和activities之间的沟通问题。Fragments的 onAttach()发生在onActivityCreate()之前,这意味着在Fragment穿件好的时候,activity不一定存在。所以,子activity 的代理父activity需要调用onActivityCreated()方法。

fragment也会根据系统的需要被创建或者杀死,所以也需要像activity那样保存数据或者状态。

fragment总是在onCreate中从bundle参数中恢复,所以一般数据模型的setter把对象赋值给bundle参数。这样在旋转屏幕时,fragment被杀死然后重建,能够获取到最近的恢复数据。

9.Layouts

相对于iOS,Android指定layouts,有利有弊。layouts被存放在res/layouts文件下的XML文件中,,

建立layout 和activity或者fragments的链接,只需要在onCreatView:时获得layout的ID即可。

layout tips

Always work in dp (density-independent pixels) instead of pixels directly.

Don’t bother nudging items for layouts in the visual editor – often the visual editor will put individual points of spacing on objects instead of adjusting the height and width as you might like. Your best bet is to adjust the XML directly.

If you ever see the fill_parent value for a layout height or width, this value was deprecated years ago in API 8 and replaced with match_parent.

10. Data

Android的数据存储和iOS差不多。

Android Shared Preference <-> iOS NSUserDefults

都是存储类型对象

Android 通过外部或者内部文件,存储或者获取文件结构。iOS,存储在documents directory中。

SQLite <-> Core Data

最重要的不同就是Android缺少Core Data对象,直接从SQLite 数据库中读取数据。


While some of the pure implementation details are very different on Android, it is very easy to bring problem-solving skills and patterns learned on iOS to bear. Who knows? Maybe understanding how Android works just a little bit better might prepare you for the next version of iOS.

你可能感兴趣的:(iOS VS Android)