`
chandler
  • 浏览: 78692 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android学习笔记(二)

阅读更多

解耦
      其实这一段的标题我不知道以什么开始为好,想来想去还是解耦这个词比较和题目。
     最早知道耦合这个词汇,还是从大话设计模式这本书里面知道的。而现在的工作是一个旧系统的维护,真的觉得紧耦合真是万恶之源。而最近学了android的开发,然后看到了android程序的文件的体系,处处都在为了解耦而努力。有那么一点点感动。
     从一个程序的角度来说,除了本身的业务逻辑之外,还有有UI,图像,字符等组成。从一个程序来说,他们之间应该是松耦合的。比方说我把界面上的一个label从“名字”,改成“用户名”,理想状态应该是不需要任何其他任何东西的(严格点,就是不需要重新编译。)但是实际操作中,除非一个系统花费很大的经历在一些基础建设上,比方说用了spring,或者自己写个框架,这个做起来很吃力。当然,系统简单的情况下,这种级别的修改也很方便。
     而android的开发,则是内置了这种解耦的框架(现在主要介绍了资源管理,而不是业务逻辑)。在android中,把资源归结为常用的3类,统一放在res文件夹下面。图像文件放在drawable,UI放在layout上面。而所有String值和其他一些值都放在Value文件下面。然后在程序中进行引用。
     这里的引用都是在于R这个类。这个类的作用是程序与资源中的一个桥梁的作用。这个类是一个包含类。其中包含了很多常量,分别对应的是res下面的一个资源。

activity的生命周期。
    这里也是主要是几个概念为主。
    首先是在Manifest.xml中的配置。其中比较特别的是<intent-filter>这个标签,不过这是以后讲的东西,所以这里也没讲太多。
    其次是Activity Stacks,其时Last-in-first-out也就是栈。
    Activity States
       Active:和用户交互的activity,通俗点就是当前你看到的页面。
       Paused:能够看得见,但是不发生交互的activity。比方说不是全屏显示的失去了焦点。
       Stopped:不可见,但是还在stacks中的。
       Inactive:移出stacks的

Monitoring State Changes和生命周期
package com.paad.myapplication; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { // Called at the start of the full lifetime. @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); // Initialize activity. } // Called after onCreate has finished, use to restore UI state @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. } // Called before subsequent visible lifetimes // for an activity process. @Override public void onRestart(){ super.onRestart(); // Load changes knowing that the activity has already // been visible within this process. } // Called at the start of the visible lifetime. @Override public void onStart(){ super.onStart(); // Apply any required UI change now that the Activity is visible. } // Called at the start of the active lifetime. @Override public void onResume(){ super.onResume(); // Resume any paused UI updates, threads, or processes required // by the activity but suspended when it was inactive. } // Called to save UI state changes at the // end of the active lifecycle. @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. super.onSaveInstanceState(savedInstanceState); } // Called at the end of the active lifetime. @Override public void onPause(){ // Suspend UI updates, threads, or CPU intensive processes // that don’t need to be updated when the Activity isn’t // the active foreground activity. super.onPause(); } // Called at the end of the visible lifetime. @Override public void onStop(){ // Suspend remaining UI updates, threads, or processing // that aren’t required when the Activity isn’t visible. // Persist all edits or state changes // as after this call the process is likely to be killed. super.onStop(); } // Called at the end of the full lifetime. @Override public void onDestroy(){ // Clean up any resources including ending threads, // closing database connections etc. super.onDestroy(); } }

    
    书中有张图。比较清晰的解释了他们之间的逻辑关系。有兴趣的可以去看一下。
    还有种记忆方法就是
    The Full Lifetime:onCreate和onDestroy 整个程序
    The Visible Lifetime:onStart和nStop     进入Active
     The Active Lifetime:onResume和onPause ,获得和失去焦点。

Android Activity Classes
       MapActivity,ListActivity,ExpandableListActivity,ActivityGroup

分享到:
评论
1 楼 doylecnn 2009-07-21  
Last-in-first-out也就是先进先出的模式
???

相关推荐

Global site tag (gtag.js) - Google Analytics