[安卓教程翻译]4.3 Stopping and Restarting an Activity

正确停止以及再启动你的activity十分重要,这可以让用户认为程序一直在运行,并没有丢失他们的进展。下面是一些你需要停止以及在启动activity的场景。

 

  用户从你的app打开另一个app,那么你app中最近处于前台的activity就进入stopped状态。如果用户从主界面或最近的应用窗口返回你的app,该activity就重启动。

  如果用户在你的app中执行了某项操作打开了新的activity,之前的activity在新activity被创建时就进入了stopped状态。如果用户按下返回键,之前的activity就重启动。

  用户在使用你的app时接到来电。

 

Activity类提供了两个生命周期函数,onStop()和onRestart(),使你能在activity进入stopped和restarted状态时能够进行明确的操作。与部分UI被遮挡的paused状态不同,stopped状态下UI完全不可见,用户的焦点处于一个单独的activity上(或一个完全独立的app)。

 

注意:由于在stopped状态下系统内存将保留你的Activity对象,所以可能你并不需要完成onStop()以及onRestart()方法(甚至连onStart()也完全不需要。由于大多数activity相对简单,这些activity都会正常地stop和restart,你仅仅需要使用onPause()方法来暂停正在进行的动作以及释放掉系统资源)。

 

Stop Your Activity

 

当activity调用onStop()方法,该activity将不再可见,并且应该释放掉几乎所有在用户不使用时不必要的资源。一旦activity进入stopped状态,系统在内存不足时可能会销毁该实例。极端情况下,可能在没有调用onDestroy()方法就直接杀死应用程序进程,所以使用onStop()方法来释放资源,防止内存泄露是十分重要的。

 

尽管onPause()方法在onStop()方法之前被调用,你还是应该使用onStop()来执行更大型,占用CPU更多的关闭操作,比如向数据库写入信息。

 

@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.
            );
}

当activity进入stopped状态,Activity对象仍然留在系统内存中,当activity恢复时重新被调用。在进入Resumed状态之前的所有回调函数中创建的东西都不需要再次初始化。系统也会锁定布局中每个组件的当前状态,所以如果用户正在EditText中编辑信息,其内容会被保留,你不必存储并恢复它们。

 

Start/Restart Your Activity

 

当activity从stopped状态重新回到前台,它将调用onRestart()。系统也会调用onStart()方法,该方法在activity每次变得可见时都会被调用(无论是被restarted或是第一次被创建的时候)。与此相比,onRestart()方法只在activity从stopped状态恢复时被调用,所以当且仅当activity之前进入stopped状态又没有被销毁时,你可以用onRestart()方法处理一些特定的恢复工作。

 

很少见到应用程序使用onRestart()来恢复activity的状态,所以大多数app没有该方法的参考。然而,由于onStop()方法本质上清理了几乎所有activity资源,当activity重新启动,你需要重新实例化它们。当activity第一次被创建时你同样要实例化它们。为此,你应把onStart()方法当作onStop()方法的配对来使用它,因为activity第一次被创建或是从stopped状态重启动时都会调用onStart()方法。

 

例如,在回来之前用户可能已经离开你的app很长一段时间,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    
}

系统销毁activity时将调用onDestroy()方法。由于通常情况下你在onStop()中释放了大部分资源,当onDestroy()被调用,可能没有什么工作要做了。该方法是你释放资源,防止内存泄露的最后一站,所以要确保附加线程都被销毁,其他长时间运行的操作被终止。

 

 

 

 

 

 

posted @ 2014-03-07 10:44  沙发土豆  阅读(109)  评论(0)    收藏  举报