`

[Android Training视频系列]2.3 Stopping and Restarting an Activity

阅读更多
1.主要内容

本小节介绍onStop与onRestart以及onStart的使用,通过本讲我们能学会应该在onStop与onStart里面执行什么样的操作。
2.观看视频讲解
http://www.eyeandroid.com/thread-11346-1-1.html

3.翻译参考
停止与重启Activity

在activity生命周期中,恰当的停止与重启activity是很重要的,这样能确保用户感知到程序的存在并不会丢失他们的进度。在下面一些关键的场景中会涉及到停止与重启:

   .用户打开“最近使用的程序(Recent Apps)”的菜单并从当前app切换到另外一个app,这个时候先前的app是被停止的。如果用
户通过“主屏幕加载图标(Home screen launcher icon”或“最近使用的程序(Recent Apps)”重新回到这个app,则activity会重启。

   .用户在当前的app里面执行启动一个新的activity的操作时,当前activity会在第二个activity被创建后停止。如果用户点
击back按钮,之前的activtiy会被重启。

   .用户在运行app时接受到一个来电通话。
Activity类提供了onStop()方法与onRestart()方法用于在activity停止与重启时进行调用。和暂停状态时部分阻塞用户接口不同,停止状态时UI不可见并且用户的焦点转移到另一个activity中。

|注意:因为系统在Activity停止时会在内存中保存了Activity实例,所以很多时候不需要实现onStop()方法与onRestart()方法,甚至是onStart()方法。 因为大多数的Activity相对比较简单,Activity会自动正常停止与重启,只需要使用onPause()来停止正在运行的动作并断开系统资源链接。





如图1所示:当用户离开Activity时系统会调用onStop()来停止Activity (1)。这个时候如果用户返回,系统会调用onRestart()(2),
紧接着会调用onStart()(3)与onResume()(4)。需要注意的是:无论什么原因导致Activity停止,系统总是会在onStop()之前调用
onPause()方法。
停止Activity

当activity调用onStop()方法时,该activity不再可见并且应该释放所有不再需要的资源。一旦activity停止了,系统可能会摧毁activity的实例以回收内存,甚至,系统会不执行activity的onDestroy()回调方法而直接杀死你的app进程, 因此需要使用onStop()来释放资源,从而避免内存泄漏。

尽管onPause()方法是在onStop()之前调用,通常应该使用onStop()来执行CPU密集型的关闭操作,例如把数据写入数据库。

例如,下面是一个在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.
            );
}

当Activity已经停止,Activity对象会保存在内存中,并且在Activity恢复的时候被重新调用到。不需要在恢复到Resumed state状态前重新初始化那些被保存在内存中的组件。系统同样保存了每一个在布局中的视图的当前状态,如果用户在EditText组件中输入了文本,也会被保存,因此不需要保存与恢复它。

注意:即使系统会在Activity停止的时候销毁这个Activity,系统仍然会保存视图对象(如文本编辑框中的文本)到一个Bundle中,并且在用户返回这个Activity时恢复他们(下一节会介绍在Activity销毁与重建时如何使用Bundle来保存其他数据的状态)。

启动与重启Activity

当Activity从Stopped状态回到前台时会调用onRestart(),系统再调用onStart()方法,onStart()方法在每次Activity可见时都会被调用。onRestart()方法则是只在Activity从stopped状态恢复时才会被调用,因此可以使用它来执行一些特殊的恢复工作,请注意之前是被stop而不是destrory。

使用onRestart()来恢复Activity状态并不常见,因此对于这个方法如何使用没有指导说明。但是,由于onStop()方法要做清除所有Activity资源的操作,在重新启动Activtiy时需要重新实例化被清除的资源,同样,在Activity第一次创建时要实例化那些资源。因为系统会在创建Activity与从停止状态重启Activity时都会调用onStart(),应该使用onStart()作为onStop()所对应的方法。

例如:因为用户很可能在回到Activity之前需要过一段时间,所以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   
}
当系统Destory一个Activity,它会为Activity调用onDestroy()方法。由于会在onStop方法里面做释放资源的操作,而onDestory方法则是最后去清除那些可能导致内存泄漏的地方,因此需要确保那些线程都被Destroy并且所有的操作都被停止。
  • 大小: 60.2 KB
1
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics