01_translation_avtivity生命周期04

来源:互联网 发布:小甲鱼python好吗 编辑:程序博客网 时间:2024/05/01 18:43

Stopping and Restarting an Activity  停止和重新启动一个Activity

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:

适当的停止和重新启动你的activity是一个重要的流程活动生命周期,确保您的用户感知应用程序总是活着,不失去他们的进程。有一些活动停止并重新启动的关键的场景:

  • 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 theBack 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() andonRestart(), 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 theonStop() andonRestart() (or evenonStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to useonPause() to pause ongoing actions and disconnect from system resources.

注意:由于系统保留你的activity实例时系统内存时停止,可能你不需要实现onStop()和onRestart()(onStart())方法。对于大多数活动相对简单,活动将停止并重启,你可能只需要使用onPause()暂停正在进行的动作和断开系统资源。

When the user leaves your activity, the system callsonStop() to stop the activity (1). If the user returns while the activity is stopped, the system callsonRestart() (2), quickly followed byonStart() (3) andonResume() (4). Notice that no matter what scenario causes the activity to stop, the system always callsonPause() before calling

onStop().

当用户离开你的活动,系统onStop()(1)停止活动。如果用户返回活动停止,系统onRestart()(2),随后很快onStart()(3)和onResume()(4)。请注意,不管什么情况导致活动停止,系统总是在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 finalonDestroy() callback, so it's important you useonStop() to release resources that might leak memory.

当你的活动调用onStop()方法,它不再是可见的,应该释放几乎所有资源,不需要用户再使用它。一旦你的活动停止,如果需要恢复系统内存,系统可能会破坏实例。在极端的情况下,系统可能会没有调用活动的最终onDestroy()回调简单地杀死你的应用过程,所以重要的是使用onStop()来释放资源,可能会泄漏内存。

Although the onPause() method is called beforeonStop(), you should useonStop() 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:

举例来说,这里的onSto()p的实现保存草稿的内容,注意持久性存储:

@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 eachView in the layout, so if the user entered text into anEditText 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 theView objects (such as text in anEditText) in aBundle (a blob of key-value pairs) and restores them if the user navigates back to the same instance of the activity (thenext lesson talks more about using a Bundle to save other state data in case your activity is destroyed and recreated).

注意:即使系统破坏你的活动,虽然停了下来,它仍然保留对象的状态(比如文本EditText)Bundle(-值对的一个blob)和恢复他们如果用户导航回相同的实例的活动(第二课会谈更多关于使用包保存其他状态数据以防你的活动被销毁并重新创建)

Start/Restart Your Activity 启动/重新启动activity

When your activity comes back to the foreground from the stopped state, it receives a call toonRestart(). 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 restoration work that might be necessary only if the activity was previously stopped, but not destroyed.

当你的活动从停止状态回到前台,它接收电话toonRestart()。系统也称theonStart()方法,它发生的每一次你的活动变得可见(是否重启或创建的第一次)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 youronStop() 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 theonStart() callback method as the counterpart to theonStop() method, because the system callsonStart() 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, theonStart() 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 yourActivity. Because you should generally have released most of your resources withonStop(), by the time you receive a call toonDestroy(), 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.

 当系统破坏你的活动时,它调用onDestroy()方法。因为你一般应该调用onStop()释放你的大部分资源,onDestroy()不是大多数应用程序需要做的事情。这种方法是你最后一次清理资源的机会,可能导致内存泄漏,所以你应该确保额外的线程被破坏和其他长期方法跟踪等操作也停止了。

 

0 0
原创粉丝点击