Activity基础

Activity相当于一个用户界面。

需要我们处理一些系统回调方法,其中Activity生命周期回调有,onCreate()、onRestart()、onStart()、onResume()、onPause()、onStop()、onDestroy()。

当系统资源不足时,系统可以杀死未激活的Activity,所以应在onPause()保存关键数据,onStop()和onDestroy()可能执行不到,但这样又会降低用户体验的流畅度。

保存Activity状态的回调方法是onSaveInstanceState(),所以每次create时会有个状态的数据传进来,即onCreate()方法有参数onCreate(Bundle savedInstanceState)。
When the screen orientation changes, the system destroys and recreates the activity in order to apply alternative resources that might be available for the new orientation. For this reason alone, it's very important that your activity completely restores its state when it is recreated, because users regularly rotate the screen while using applications.

Activity基础_第1张图片
一个任务就是一些Activity的集合,栈。
When the user leaves a task by pressing the HOME key, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.
这就给人一种行云流水的感觉,而不是每个应用程序都是分开的。
Activity基础_第2张图片
简而言之activity和tasks的行为:
当Activity A 开启 Activity B,Activity A 就停止了,但是系统保留它的状态(如位置和表格中键入的文本)。如果在activity B中用户按下BACK键Activity A 重新再入他的状态,恢复运行。当用户通过按下HOME键离开一个Task时,当前的Activity 停止,对应的Task进入后台。系统保留该Task中每个activity的状态。如果用户稍后稍后恢复运行该Task,该Task回到前台,恢复运行其栈顶部的activity。 如果用户按下BACK键,当前的activity从栈中弹出销毁。栈中的前一个activity恢复运行。当activity销毁时,系统不保留这个activity的状态。activity可以被实例化多次,即使是其他任务的activity。

如果需要控制task,可以通过manifest文件中<activity>的属性或者将带有特定标志的intent传递给startActivity()来改变默认行为。

参看Android Developers官网

你可能感兴趣的:(android,Activity)