Android官方入门文档[14]停止和重新启动一个Activity活动
Stopping and Restarting an Activity
停止和重新启动一个Activity活动
This lesson teaches you to
1.Stop Your Activity
2.Start/Restart Your Activity
You should also read
?Activities
这节课教你
1.停止您的Activity活动
2.启动/重新启动您的Activity活动
你也应该阅读
?Activity活动
Try it out
Download the demo
ActivityLifecycle.zip
试试吧
下载演示
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.
正确停止和重新启动Activity活动是在Activity活动周期,以确保您的用户感知到你的应用程序始终是活着,不会失去他们的进程的一个重要过程。还有的在您的Activity活动停止和重新启动的关键场景的几个:
?用户打开从应用程序最近的窗口和切换你的应用程序到另一个应用程序。在您的应用程序的Activity活动,是当前在前台停止。如果用户返回到从主屏幕启动器图标或最近的应用程序窗口,该Activity活动将重新启动您的应用程序。
?用户在执行您的应用程序启动一个新的Activity活动的行动。在创建第二activity时,当前Activity活动停止。如果用户然后按压后退按钮时,第一个Activity活动被重新启动。
?在使用他或她的电话您的应用程序的用户将收到一个电话。
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 handlesActivity活动柄被停止和重新启动。不像暂停状态,其中确定一个局部的UI阻塞,停止状态保证在UI不再可见和用户的焦点是在一个单独的Activity活动(或完全独立的应用程序)。
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活动都是。比较简单,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活动停止,系统()调用之前一直的onStop调用的onPause()。
Stop Your Activity
停止你的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活动停止后,系统可能会破坏例如,如果需要恢复系统内存。在极端的情况下,系统可能只是杀了你的应用程序,而无需调用该Activity活动的最后的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.
虽然的onPause()方法的onStop()之前调用,你应该使用的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活动停止,Activity活动对象保持驻留在内存中的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).
注:即使系统破坏你的Activity活动,而它的停了下来,但仍保留在一个Bundle(键值对的BLOB)查看对象(如一个EditText文本)的状态,并恢复他们,如果用户返回该Activity活动的同一个实例(下一课谈更多关于使用Bundel捆绑,以节省的情况下你的Activity活动被破坏并重新创建其他状态数据)。
Start/Restart Your Activity
启动/重新启动您的Activity活动
--------------------------------------------------------------------------------
When your activity comes back to the foreground from the stopped state, it receives a call to onRestart(). The system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time). The onRestart() method, however, is called only when the activity resumes from the stopped state, so you can use it to perform special restoration work that might be necessary only if the activity was previously stopped, but not destroyed.
当你的Activity活动回来,从停止状态的前景,它接收调用onRestart()。该系统还调用ONSTART()方法,这恰好每次你的activity变得可见的时间(无论被重新启动或首次创建)。该onRestart()方法,但是,被称为从停止状态的Activity活动恢复,只有当,所以你可以用它来执行只有在以前activity停止,但不被破坏,可能需要特殊的修复工作。
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()恢复activity的状态,所以不会有这种方法适用于应用程序的一般人群的指引。但是,因为你的onStop()方法应该基本上是清理所有activity的资源,你需要重新实例化它们的activity重新启动时。然而,还需要在你的Activity活动是第一次创建实例化它们(当有Activity活动的任何现有实例)。出于这个原因,你通常应该使用ONSTART()回调方法作为对应到的onStop()方法,因为系统调用ONSTART()都当你创建Activity活动,并在重新启动时从停止状态的activity。
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()方法,为您的Activity活动。因为你通常应该已经发布了你的大部分资源的onStop()的时候,你接到一个电话来的onDestroy(),没有太多的大多数应用程序需要做的。这种方法是你最后的机会来清理资源,可能导致内存泄漏,所以你应该确保其他线程被破坏和其他长期运行的操作,如方法跟踪也停止。
Next: Recreating an Activity
下一页:重新创建一个Activity活动
本文翻译自:https://developer.android.com/training/basics/activity-lifecycle/stopping.html