`

[Android Training视频系列]2.2 Pausing and Resuming an Activity

阅读更多
1.主要内容
本讲介绍onPause和onResume,主要分析了在onPause函数中应该执行哪些操作。

2.视频讲解
http://www.eyeandroid.com/thread-11291-1-1.html

3.翻译参考
在正常的程序使用中,处于前台运行的activity有时会被另外一些可见的组件遮挡,从而造成activity被暂停。比如,一个半透明的activity(类似一个对话框)被打开,先前那个activity就会被暂停。虽然这个activity还有部分可见,但是当前的焦点不在这个activity上,因此它保持在paused状态。

不过,一旦这个activity被全部遮挡,处于不可见的状态,那么它就是被停止了。(下个教程讨论)

如果你的activity要进入paused状态,系统会调用你的activity种的onPause()方法,这个方法让你能够暂停那些不应该继续运行的行为(比如暂停视频播放),或者保存那些应该永久保存的用户数据。一旦用户从paused状态返回,系统能够调用onResume()方法恢复以前的运行。

提示:当activity接收调用onPause()时,这可能表明activity要暂时进入paused状态,用户可能马上就重新获得activity的焦点。但是,它也是用户离开这个activity时会调用的第一个方法。




图解:当一个半透明的activity覆盖你的activity时,系统调用onPause(),activity就会在Paused状态中等待(1)。系统调用onResume()方法返回activity。

暂停你的activity
当系统调用onPause()方法的时候,技术上说明activity还是能部分可见的,但是大部分情况表明用户要离开activity,它将进入Stopped状态。你通常使用onPause()函数做下面的事:
停止消耗cpu的动画或者运行中的动作。
提交未保存的更改,但是只是保存用户离开后期望永久保存的东西(比如邮件草稿)。
释放系统资源,比如广播接收器,传感器(比如GPS),或者消耗电量的资源,这些资源是activity暂停后用户不再需要的。
例如,如果你的程序需要使用摄像头,onPause()方法中是释放它的好地方。

@Override 
public void onPause() { 
    super.onPause();  // 通常先调用父类方法 
 
    // 释放摄像头资源 
    if (mCamera != null) { 
        mCamera.release() 
        mCamera = null; 
    } 


通常情况下,你不需要使用onPause()方法储存用户的修改(比如填写信息的表单)到固定存储器。只有在用户真的期望自动保存数据(比如邮件草稿)的时候才需要在onPause()方法中保存。你应该尽量避免在onPause()方法中让CPU进行密集的工作,比如写入数据到数据库,因为这样会减慢你进入下一个activity的速度(你应该在onStop()中执行这些高负载的关闭操作)。

如果你的activity确实需要被停止话,你应该保持你onPause()方法尽量的简单,这样能够保证快速的转换到用户的下一个目的地。

提示:当你的activity已经被paused,这个activity实例会被保持在内存中,当activity需要被恢复的时候被调用。你不需要再重新初始化组件。

恢复你的activity
当用户从Paused状态恢复你的activity的时候,系统调用onResume()函数。

需要了解的是activity每次进入前台都会调用这个方法,包括activity被第一次创建的时候。这样的话,你必须在onResume()方法中初始化那些onPause()中释放的组件,并且执行任何需要在Resumed状态中需要的初始化工作。(比如开始动画和初始化activity前台状态需要的组件)

下面的例子和上面onPause()的例子相对应,这里初始化暂停时释放的摄像头资源。
@Override 
public void onResume() { 
    super.onResume();  // 调用父类方法 
 
    // 当activity取得用户焦点时取得摄像头实例 
    if (mCamera == null) { 
        initializeCamera(); // 摄像头初始化的本地方法 
    } 

  • 大小: 56.2 KB
0
2
分享到:
评论

相关推荐

    暂停和恢复Activity-Pausing and Resuming an Activity

    在应用程序正常时,当前的Activity有时会因为其他可视化组件阻塞(obstructed)而导致Activity暂停。例如,当打开一个半透明(semi-transparent)的活动(比如,对话框),先前的Activity就会暂停。

    Android 暂停和恢复Activity

    暂停和恢复Activity(Pausing and Resuming an Activity) 一个Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,例如拨号、拍照、发送email、看地图。每一个activity被给予一个窗口...

    PROGRAMMING ACTIONSCRIPT 3.0

    Pausing and resuming playback.628 Extending the Podcast Player example...629 Chapter 21: Capturing user input.631 Basics of user input.631 Capturing keyboard input...633 Capturing mouse input 636 ...

    PhoneGap Mobile Application Development Cookbook.pdf

    Resuming your application 134 Displaying the status of the device battery levels 138 Making use of the native search button 145 Displaying network connection status 149 Creating a custom submenu 155 ...

    labview_可以对单次输入的信号进行滤波分析和图像显示_低频率的未知信号

    It also has the functions of pausing and preserving historical images. This program is mainly for low frequency unknown signals, for high frequency signals, the best way to use a single sampling.)

    Microsoft Codeview and Utilities User's Guide

    13.3.28 Pausing during Linking (/PAU) 13.3.29 Specifying User Libraries for Quick Languages (/Q) 13.3.30 Setting Maximum Number of Segments (/SE) 13.3.31 Controlling Stack Size (/ST) 13.3.32 Issuing ...

    Android代码-ChronometerPersist

    A helper library for keeping and maintaining the state of Chronometers. How to use it? Creating an instance of ChronometerPersist val chronometerPersist = ChronometerPersist.getInstance(chronometer, ...

    ImageMagick图片批量处理

    -page geometry size and location of an image canvas (setting) -ping efficiently determine image attributes -pointsize value font point size -precision value maximum number of significant digits to...

    A Collection of Laravel Tutorials

    No more rewinding or pausing videos. Only the most detailed text instructions with open-source applications. If you prefer reading straight to the point instructions, over watching long and bloaty ...

    LICEcap.dmg-GIF屏幕录制工具

    Global hotkey (shift+space) to toggle pausing while recording Adjustable maximum recording framerate, to allow throttling CPU usage. Basic title frame, with or without text. Record mouse button ...

    Hayt Engineering Circuit Analysis 8th

    understand a complex system by its analogy to an electrical circuit. Before launching into all this, however, we’ll begin with a quick preview of the topics found in the remainder of the book, ...

    EurekaLog_7.5.0.0_Enterprise

    4)....Added "--el_injectjcl", "--el_createjcl", and "--el_createdbg" command-line options for ecc32/emake to inject JEDI/JCL debug info, create .jdbg file, and create .dbg file (Microsoft debug format...

    DOTween_1_2_135

    BUGFIX: Fix for obsolete playModeStateChanged API in Unity 2017 and later BUGFIX: Removed panels/dialogues auto-opening, which could cause bugs when building player from command line BUGFIX: Fixed ...

    FairyGUI-Unity-Plugin-3.4.0.zip

    - NEW: Transitions now support playing partially and pausing. - IMPROVED: Change the way of registering bitmap font. - FIXED: A GButton pivot issue. - FIXED: Correct text align behavior. 2.3.0 - NEW...

    Faculty-App-Scraper:屏幕抓取应用

    Faculty-App-Scraper 从旧网站上抓取 PDF 文档的快速/廉价方法。... pausing for entry of HUID- Navigate to the list of applications- "Click" on each application to downloading the PDF file - The PDF file

    Java版电子宠物游戏代码

    /** Handle pausing the MIDlet */ public void pauseApp () { } /** Handle destroying the MIDlet */ public void destroyApp (boolean unconditional) { } /** Quit the MIDlet */ public static ...

    迅雷播放器引擎(APlayer)和集成了迅雷下载引擎-易语言

    状态说明:引擎的当前状态 #PS_READY:准备就绪 // #PS_OPENING:正在打开 // #PS_PAUSING:正在暂停 // #PS_PAUSED:暂停中 // #PS_PLAYING:正在开始播放 // #PS_PLAY:播放中 // #PS_CLOSING:正在开始关闭 时长说明:...

    详解webpack的clean-webpack-plugin插件报错

    1、出错代码 const path = require('path') const CleanWebpackPlugin = require('clean-webpack-plugin') // const { CleanWebpackPlugin } = require('clean-webpack-plugin') module.exports = { ...

    muddler:基于Perl的泥浆客户,让人想起TinyFugue

    此屏幕快照显示了混乱的两个接口。 在后台,在终端窗口中运行的是Muddler。 前景窗口是通过网络套接字通过Firefox连接到同一个mudler会话的。 两者都是互动的,可以显示相同或... o More / pausing output as needed

    RH850F1L用户手册:软件

    2.2 CPU Operating Modes............................................................................................................8 2.2.1 Definition of CPU operating modes...............................

Global site tag (gtag.js) - Google Analytics