zoukankan      html  css  js  c++  java
  • android activity

    1 生命周期:

      There are three key loops you may be interested in monitoring within your activity:

    • The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().
    • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user an no longer see what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.
    • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
     
    2 四种加载模式
     
    Use CasesLaunch Mode               Multiple Instances?Comments
    Normal launches for most activities "standard" Yes Default. The system always creates a new instance of the activity in the target task and routes the intent to it.
    "singleTop" Conditionally If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to itsonNewIntent() method, rather than creating a new instance of the activity.
    Specialized launches
    (not recommended for general use)
    "singleTask" No The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to itsonNewIntent() method, rather than creating a new one.
    "singleInstance" No Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

    3 不同加载模式下生命周期测试

    新建Activity1,Activity2,Activity3,每个放置一个button
    1 点击button时跳转顺序为1->2,2->3,3->1。各Activity的launchmode为默认
      Intent i = new Intent(this, Activity2.class);
      startActivity(i);
      -->Activity1启动时顺序, onCreate1-->onStart1-->onResume1
      -->Activity1跳转到Activity2时顺序:onclick-->onPause1-->onCreate2-->onStart2-->onResume2-->onStop1,其他跳转相同
      -->2至1返回时顺序。按返回-->onPause2-->onRestart1-->onStart1-->onResume1-->onStop2-->onDestroy2
    2 当Activity3的launchmode为singleTask时,Activity1和Activity2为默认时
      -->3至3的调用顺序为 onClick-->onPause3-->onNewIntent3-->onResume3
      -->3至2的调用顺序为 onclick-->onPause3-->onCreate2-->onStart2-->onResume2-->onStop3
      -->接上步,2至3的调用顺序为onClick-->onPause2-->onNewIntent3-->onRestart3-->onStart3-->onResume3-->onStop2-->onDestroy2
    3 当Activity3的launchmode为singleTop时,Activity1和Activity2为默认时
      -->3至3的调用顺序为 onClick-->onPause3-->onNewIntent3-->onResume3
      -->3至2的调用顺序为 onclick-->onPause3-->onCreate2-->onStart2-->onResume2-->onStop3,其他跳转相同
    4 当Activity2的launchmode为singleInstance时,Activity1和Activity2为默认时
      -->3至3的调用顺序为 onClick-->onPause3-->onNewIntent3-->onResume3
      -->3处按back键调用顺序为 按返回-->onPause3-->onRestart1-->onStart1-->onResume1-->onStop3-->onDestroy3 直接跳过2,因为2不在同一个栈内
      -->3至2的调用顺序为:onClick-->onPause3-->onNewIntent2-->onRestart2-->onStart2-->onResume2-->onStop3

     4 SingleTask模式

    Activity1,Activity2(singleTask),Activity3
    每个放置一个button,分别为1->2,2->3,3->2
    当调用singleTask类型的Activity时,若调用者和被调用者的android:taskAffinity值相同,会在原先栈中产生Activity,而不是新栈。  
    当Activity2为和1不同后,Activity2会在新栈中产生,3->2时,因Activity2在此栈中已存在,将栈内的其上的Activity清除,Activity2显示。如下图所示:

     


        

    资料:Android四种Activity的加载模式

         Android下的任务和Activity栈

         解开Android应用程序组件Activity的"singleTask"之谜

              Android API:Activity

  • 相关阅读:
    C#调用存储过程带输出参数或返回值
    车辆售票坐位图
    C#操作SQL Server通用类
    Java基础知识总结
    Maven 安装与配置
    读取文件内容
    复制一个文件
    求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
    输入一个递增的顺序排序的二维数组和一个整数,判断数组中是否含有该整数
    在由N个元素构成的集合S中,找出最小元素C,满足C=A-B,其中A,B是都集合S中的元素,没找到则返回-1
  • 原文地址:https://www.cnblogs.com/myparamita/p/2245001.html
Copyright © 2011-2022 走看看