Activity生命周期之stop和restart Activity

以下是一些Activity 停止和重新启动的情况:
  • 突然接到电话
  • 当用户从一个A Activity启动另一个B Activity时候,A处于Stopped,当按返回键时,B被restart
  • 用户打开最近应用窗口,从你的app切换到别的app的时候。Activity处于Stopped,等到再切换回来时,Activity被restart
    注意:因为系统会将你的Activity缓存在内存中,所以有可能,你不用继承onRestart()(甚至是onStart()),特别简单的应用,可以只用onPause()来释放资源。
  • 当用户离开Activity时系统会调用onStop(),当用户再回来时,系统调用onRestart() 然后快速调用onStart()和onResume()。无论什么情况导致Activity 被stopped,都会先调用onPause(),然后调用onStop()

stop你的Activity

当你的Activity接受到onStop(),就会变为不可见。这时候需要释放几乎所有的资源。当Activity处于stopped状态,系统可能会因为回收系统内存资源而销毁Activity缓存的实例。极端的情况,系统可能会不回调onDestroy()而直接杀死你的程序进程。所以在onStop()中释放可能导致内存泄露的资源非常重要
尽管onPause()是在onDestroy()之前。你应该用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. 
            ); 
} 

注意:即使系统会销毁你的处于stopped状态的Activity,但是它人然会储存视图对象的状态(如:EditText中的文本)在bundle中,当用户回到同一个Activity实例时系统会恢复这些数据。(下篇文章会介绍如何使用bundle来存储状态信息)

restart并且start你的Activity

当你的Activity从Stopped状态恢复Resumed状态,会回调onRestart()。然后调用onStart()。每次Activity回到可见状态都需要调用onStart()函数。只有从stopped状态回到visible状态才会调用onRestart()。
用户可能离开你的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     
} 

一般不用onRestart()函数来恢复Actitiy的状态。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android应用程序中,Activity(活动)是用户界面的主要组成部分,每个Activity都有自己的生命周期,这是Android操作系统管理应用程序资源的关键部分。Activity生命周期可以分为多个阶段,从创建到销毁,包括以下几个主要阶段: 1. **创建(Creation)**:当用户启动一个新的Activity或从任务栈返回时,系统会调用`onCreate()`方法,这是Activity第一次被创建并加载到内存中的时刻。 2. **初始化(Initialization)**:`onCreate()`之后可能会调用`onStart()`,表示Activity已经准备好接受用户的输入,但不一定可见。如果它是当前任务的顶级Activity,还会调用`onResume()`,使得Activity变为可见。 3. **运行(Running)**:`onResume()`后,Activity处于活动状态,可以接收到用户的输入事件,此时它是可见的并且在屏幕上显示。 4. **暂停(Pause)**:当设备的方向改变、系统需要更多的内存或者用户切换到另一个应用程序时,Activity会被暂停,`onPause()`方法会被调用。暂停期间,所有的输入都将被挂起。 5. **停止(Stop)**:即使暂停,系统也可能调用`onStop()`,表示Activity不再接收用户输入,但仍存在于内存中。 6. **重建(Restart)**:当设备旋转等导致需要重新创建Activity时,系统会先调用`onSaveInstanceState()`保存数据,然后调用`onCreate()`和`onStart()`。 7. **恢复(Restore)**:如果Activity之前被销毁,那么在返回时会先调用`onRestoreInstanceState()`恢复数据,再执行后续生命周期方法。 8. **可见(Visible)**:如果Activity重新变得可见,会调用`onRestart()`,然后`onStart()`,最后`onResume()`。 9. **隐藏(Hidden)**:用户切换到其他应用或按Home键,但该Activity仍然在后台,`onPause()`会被调用。 10. **销毁(Termination)**:当用户完全退出应用程序,或者系统需要释放内存空间时,会依次调用`onStop()`、`onSaveInstanceState()`、`onDestroy()`,Activity被完全关闭,释放所有资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值