您的位置:首页 > 移动开发 > Android开发

Android---Activity 生命周期(三)Stopping Activity && Restarting Activity

2015-06-12 10:11 525 查看
1》There are a few of key scenarios in which your activity is stopped and restarted:

<翻译>Activity被停止(stopped)和重启(restarted)的一些情形:

1.1》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.

1.2》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.

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

2》Stopping Activity && Restarting Activity



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().

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.

<翻译>在极端情况下,系统可能不调用最后一个生命周期函数onDestroy()就直接终止你的app进程,所以在onStop中释放可能会引起内存泄露的资源是很重要的。

Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.

<翻译>尽管在调用onStop()方法之前会调用onPause()方法,你应该在onStop()方法执行像往数据库里写数据这样的耗时,大计算量的停止操作( larger, more CPU intensive shut-down operations)。

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes.

You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.

<翻译>不必重新初始化那些直到Resumed状态所调用的生命周期函数中所创建的组件。

The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.

<翻译>系统同时也记录布局中每个View中的当前状态,如果用户在EditText中输入信息了,那这些信息会被保留下来,所以你不必再保存和恢复它了。

Note: Even if the system destroys your activity while it's stopped, it still retains the state of the View objects (such as text in an EditText) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the
activity (the next lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated).

<翻译>注意:就算系统在Activity处于Stopped状态时销毁了它,系统也会在Bundle(一个键-值对)保存View的状态,并在用户返回到同一个Activity实例中时恢复它们(下一节将着重讨论使用Bundle来保存其他的状态数据以防Activity被停止和重新创建)。

3》Start/Restart Your Activity

However, because your onStop() method should essentially clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. Yet, you
also need to instantiate them when your activity is created for the first time (when there's no existing instance of the activity).

For this reason, you should usually use the onStart() callback method as the counterpart to the onStop() method, because the system calls onStart() both when it creates
your activity and when it restarts the activity from the stopped state.

ps:通常onStrart()方法和onStop()方法配套使用,即在onStop中释放的资源,要在onStart()方法中再初始化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: