合理安排Activity生命周期中停止和重新启动的时机,能够使得让用户意识到应用软件始终处于运行状态,并维持着他们的进程。下面列举了若干Activity停止和重新启动场景:
- 用户通过“最近使用的应用软件”窗口从本应用软件切换到其他应用软件,运行在前台的Activity被停止。当用户通过HOME屏幕启动图标或者“最近使用的应用软件”窗口返回本应用软件,Activity被重新启动。
- 用户通过应用软件的某个动作启动另一个Activity,当前Activity在新的Activity被创建时被停止。当用户点击Back按钮,被停止的Activity被重新启动。
- 当用户在使用应用软件时接听电话。
Activity类提供了OnStop()和OnRestart()两个生命周期方法,用来处理停止和重新启动的事件。与paused状态下Activity被部分遮挡不同,stopped状态下的activity完全不可见,用户焦点转到其他Activity或者其他的应用软件。
注意:由于系统在停止Activity时会自动在系统内存中保持Activity实例,OnStop()和OnRestart()(甚至是OnStart())并不总是需要开发者来具体实现。对于大部分相对简单的Activity,不实现OnStop()和OnRestart(),它们也能工作正常,开发者仅需实现OnPause()方法来暂停当前运行的动作和释放系统资源。
停止Activity
当Activity接收到对其OnStop()方法的调用,它将变为不可见,并释放不再需要的资源。Activity一旦处于Stopped状态,系统有可能会为了恢复系统内存而将其实例销毁。在极端的情况下,系统将忽略对OnDestroy()方法的调用而直接结束应用软件,所以在OnStop()方法中释放各种资源能够有效地确保内存泄露的情况发生。
尽管OnStop()之前会有OnPause()方法执行,但是复杂的、CPU占用率高的操作都应该放在OnStop()中实现,如向数据库写信息。
下面的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 = newContentValues();
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状态,Acitivity对象仍位于系统内存,当Activity重新开始时能够被重新调用。在Resumed状态的前置回调方法中创建的部件都不需要开发者额外地重新初始化。Android系统同时还会跟踪界面布局中所有View的当前状态,因此当用户向一个EditText部件输入文字,其内容由系统自动保持而不用开发者进行保存和恢复。
注意:即使系统销毁了处于Stopped状态的Activity,View对象的状态(如EditText中的文字)也会通过Bundle对象(键值对集)保持,并且在用户通过导航重新返回到该Activity同一实例时由系统恢复(下一课程将详细介绍使用Bundle保存数据以防Activity被销毁或者重建)。
启动/重新启动Activity
当Activity从Stopped状态重新返回到前台运行,它将收到OnRestart()的调用。系统会在Activity转为可见(重新启动或者第一次创建)时调用OnStart()方法,而OnRestart()方法仅会在Activity从Stopped状态恢复时被调用。因此OnRestart()可以被用来完成仅当Activity是从Stopped状态恢复而不是销毁后所需要执行一些特殊的恢复工作。
使用OnRestart()方法来恢复Activity状态的做法并不常用,因此在此没有提供通过OnRestart()来控制应用软件状态变更的指导。但是,由于OnStop()方法清理了Activity的所有资源,当Activity重新启动时需要将这些资源重新实例化。此外,当Activity在初次创建时也需要执行相同的操作。所以,OnStart()方法常常用来作为OnStop()的配对操作,因为在Activity初次创建和从Stopped状态恢复运行时Android系统都会调用OnStart()方法。
例如,由于用户有可能离开应用软件很长一段时间后再返回,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
}