Stopping and Restarting an Activity
停止和重新启动活动
This lesson teaches you to
- Stop Your Activity 停止您的活动
- Start/Restart Your Activity 开启/重新开启您的活动
You should also read
Try it out
ActivityLifecycle.zip
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.
用户在他或者她的手机上使用您的应用程序时接听到一个电话。
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()方法,他允许您专门去处理您的被停止和重新启动的活动。不像暂停状态那样,标识部分界面阻塞,停止状态保证UI不再是可见的了,以及用户的焦点是一个单独的活动(或者是一个完全独立的应用程序)。
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()方法)。对于大多数活动相对简单,活动将停止并重启好,你可能只需要使用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()
.
图一所示:当用户离开您的活动,系统回调onStop()方法停止活动(1)。如果当活动被停止时,用户返回了,系统将回调onRestart()方法,而onStart()
(3) 和 onResume()方法将紧随其后。注意,不管什么情况导致活动停止,系统总是会在回调
onStop()
方法之前回调onPause()
方法。
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 neededwhile 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 useonStop()
to release resources that might leak memory.
当您的活动收到回调onStop()
方法的信息时,当用户不在使用它的时候,它不再是可见的而且应该释放了几乎所有的资源。一旦您的活动被停止,如果需要回复系统内存,系统可能会破坏实例。在极端的情况下,系统可能会简单地杀死您的应用程序过程,而没有回调onDestroy()方法。因此您使用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()方法来执行更大、更多的CPU密集型关闭操作,离去将信息写入数据库中。
For example, here‘s an implementation of onStop()
that saves the contents of a draft note to persistent storage:
例如,下面有一个使用onStop()
方法来持久性存储草稿内容:
@Override protected void onStop() { super.onStop(); // Always call the superclass method first // Save the note‘s current draft, because the activity is stopping // and we want to be sure the current note progress isn‘t lost. ContentValues values = new ContentValues(); values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText()); values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle()); getContentResolver().update( mUri, // The URI for the note to update. values, // The map of column names and new values to apply to them. null, // No SELECT criteria are used. null // No WHERE columns are used. ); }
- 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. 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.
当您的活动被停止,Activity对象被一直驻留在内存中然后当活动恢复是被回调。您不需要重新创建那些回到恢复状态时的回调方法的初始化组件。该系统还跟踪当前状态的每个视图的布局。因此用户输入文本到一个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).
注意:即使当您的活动停止是,系统销毁了它,它仍然保持在Bundle(一组键值对)下View
对象(如EditText部件下的文本)状态,如果用户导航回复相同实例还会恢复它们。(下节课会谈更多关于使用包保存其他状态数据以防你的活动被销毁并重新创建)。
Start/Restart Your Activity
开启/重新开启您的活动
When your activity comes back to the foreground from the stopped state, it receives a call to
onRestart()
. The system also calls theonStart()
method, which happens every time your activity becomes visible (whether being restarted or created for the first time). TheonRestart()
method, however, is called only when the activity resumes from the stopped state, so you can use it to perform special restorationwork that might be necessary only if the activity was previously stopped, but not destroyed.
当您的活动从停止状态回到前台,它将收到调用onRestart()方法的信息。系统也将回调onStart()方法,这些都是每次您的活动中都是会发生的(是否重启或者首次创建)。onRestart()方法只有当活动从停止状态恢复,您可以使用它来执行特殊的修复工作,这些修复工作可能只有在活动被停止之前被需要,而不是销毁。
It‘s uncommon that an app needs to use onRestart()
to restore the activity‘s state, so there aren‘t any guidelines for this method that
apply to the general population of apps. 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.
不寻常的情况下,应用程序需要使用onRestart()
方法来恢复活动的状态,因此没有任何指导方法适合于普通人群使用。但是,由于您的onStop()方法,应该基本上清理你所有活动的资源,当活动开启时,您将需要实例化它们。然而,您的活动被第一时间创建时(如果没有活动的实例),您也需要实例化它们。基于这个原因,您通常应该使用onStart()回调方法与onStop()
方法相对应,因为当他创建您的活动或者从停止状态重新启动活动是系统会调用onStart()
方法。
For example, because the user might have been away from your app for a long time before coming back it, the
onStart()
method is a good place to verify that required system features are enabled:
例如,因为在返回您的应用程序之前,用户可能会离离开很长时间,onStart()方法是一个很好的方式来验证所需的系统功能的启用:
@Override protected void onStart() { super.onStart(); // Always call the superclass method first // The activity is either being restarted or started for the first time // so this is where we should make sure that GPS is enabled LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsEnabled) { // Create a dialog here that requests the user to enable GPS, and use an intent // with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action // to take the user to the Settings screen to enable GPS when they click "OK" } } @Override protected void onRestart() { super.onRestart(); // Always call the superclass method first // Activity being restarted from stopped state }
When the system destroys your activity, it calls the onDestroy()
method for your
Activity
. Because you should generally have released most of your resources with
onStop()
, by the time you receive a call to
onDestroy()
, there‘s not much that most apps need to do. This method is your last chance to clean out resources that could lead to a memory
leak, so you should be sure that additional threads are destroyed and other long-running actions like method tracing are also stopped.
当系统破坏您的活动是,会为您的Activity调用onDestroy()方法。因为一般情况下,您应该使用onStop()方法释放您的大多数资源,同时您会收到onDestroy()回调的信息,大多数引用程序需要做的并不多。这种方法是你最后一次机会清理资源的方法,这可能导致内存泄漏,所以你应该确保额外的线程被破坏和其他长期方法跟踪等操作也停止了。
Stopping and Restarting an Activity 停止和重新启动活动,布布扣,bubuko.com