Managing the Activity Lifecycle---Stopping and Restarting an Activity

  次阅读 来源:互联网(转载协议) 2016-01-14 10:44 我要评论(0)

----------------------------------------------------------------------------------------------------------------------

Properly stopping and restarting your activity is an important process in the activity lifecycle that ensures your users perceive that your app is always alive and doesn't lose their progress. There are a few of key scenarios in which your activity is stopped and restarted:

The user opens the Recent Apps window and switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts.

The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.

The user receives a phone call while using your app on his or her phone.

在activity的生命周期里,合适地停止和重启你的activity是一个非常重要的过程,这确保你的用户可以感到你的app总是活跃的而不是丢失了他们的进程。在以下几种情况下你的activity需要被停止或是重启:

用户打开了“最近使用的应用程序”窗口,然后从你的app切换到另一个app。那么你的app里正在前台运行的activity就停止了。如果用户从主菜单里的开启图标或者“最近使用的应用程序”窗口返回到你的app,那activity就会重启。

用户在你的app里执行了一个开始新的activity的动作。那么当前的activity在第二个activity创建时就停止了。如果用户后来就按下了返回按钮,那么第一个activity就会重启。

用户在使用你的app时接到了一个电话。

The Activity class provides two lifecycle methods, onStop() and onRestart(), which allow you to specifically handle how your activity handles being stopped and restarted. Unlike the paused state, which identifies a partial UI obstruction, the stopped state guarantees that the UI is no longer visible and the user's focus is in a separate activity (or an entirely separate app).

Activity类提供了两个生命周期方法,onStop() 和 onRestart(),它们允许你明确地控制你的activity该如何停止和重启。和暂停状态(只是局部的UI被阻塞)不同,停止状态确保UI对用户不再可见,而且用户的焦点在单独的一个activity上(或是完全是另一个单独的app)。

Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources.

注意:因为当你的Activity实例停止时系统在内存中仍然保留了它,你可能不需要实现onStop() 和 onRestart()(甚至是onStart()方法。对于大多数相对简单的activity,它们可以好好地停止和重启,而你可能只需要使用onPause()来暂停正在进行中的动作并且和系统资源分离)。

Figure 1. When the user leaves your activity, the system calls onStop() to stop the activity (1). If the user returns while the activity is stopped, the system calls onRestart() (2), quickly followed by onStart() (3) and onResume() (4). Notice that no matter what scenario causes the activity to stop, the system always calls onPause() before calling onStop().

图1. 当用户离开你的activity时,系统调用onStop()来停止activity (1)。如果当activity停止时用户再次返回,系统调用onRestart() (2),紧接着调用onStart() (3)和onResume() (4)。你应该注意到无论哪种情况下使得activity停止,系统都将先调用onPause()再调用onStop()。

Stop Your Activity —— 停止你的活动

When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

当你的activity收到一个onStop()方法的调用时,它将不再可见并且应该释放几乎所有用户在不使用它的所不需要的资源。一旦你的activity停止,如果系统需要回收系统内存,它就可能会销毁这个实例。在极端情况下,系统可能会杀死你的app进程而不需要调用activity的终极onDestroy()回调函数,因为使用onStop()来释放那些可能造成内存泄露的资源是很重要的。

本站部分文章来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,是出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如果您有什么意见或建议,请联系QQ28-1688-302!

人工智能实验室
相关文章相关文章
  • Oculus公布原型机,大幅度提升可视角,能实现140°的视场水平

    Oculus公布原型机,大幅度提升可视角,能实现140°的视场水平

  • teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

    teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

  • 2018深圳国际人工智能展览会 2018 shenzhen International Artif

    2018深圳国际人工智能展览会 2018 shenzhen International Artif

  • Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

    Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

网友点评网友点评
阅读推荐阅读推荐

据外媒报道,STEER打造了首款完全自动驾驶停车技术,旨在使常规车辆转变为无人驾驶车辆。STEER的首款技术应用是4级自动驾驶及网络安全停车...

近日,美国软性机器抓手制造商 Soft Robotics 宣布,获得 2000 万美元的融资,本轮投资者包括 Scale Venture Partners,Calibrate Ventures...

据外媒报道,加州车管局发布了《2017自动驾驶脱离报告(California Autonomous Vehicle Disengagement Reports)》,其中谈及了脱离的具体...

用人机语音交互,来解决智能家居适老的问题;通过家庭门禁与安防套件、空气净化套件、可燃气体与有害气体监控套件等相互联动,在不同生活情...