`
guzizai2007
  • 浏览: 362722 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Activity1.3

 
阅读更多

管理Activity的生命周期

你可以通过调用finish() 来终止activity

 

An activity can exist in essentially three states:

Resumed:The activity is in the foreground of the screen and has user focus(在屏幕的前面或者获取用户焦点)

 

Paused:Another activity is in the foreground and has focus, but this one is still visible,the Activity object is retained in memory, it maintains all state and member information, but can be killed by the system in extremely low memory situations.

 

Stopped:The activity is completely obscured(遮盖) by another activity,the activity is now in the "background",A stopped activity is also still alive ,it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere

 

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process(调用finish或者直接kill)

 

 

onCreate():Called when the activity is first created.create views, bind data to lists, and so on,This method is passed a Bundle object containing the activity's previous state, if that state was captured ,Always followed by onStart().

 

onStop()Called when the activity is no longer visible to the user,This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

 

onRestart()Called after the activity has been stopped, just prior to it being started again.

 

onStart()Called just before the activity becomes visible to the user.

 

onResume()Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

 

onPause()Called when the system is about to start resuming another activity,This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on,It should do whatever it does very quickly, because the next activity will not be resumed until it returns

 

onDestroy()This is the final call that the activity will receive.It could be called either because the activity is finishing , or because the system is temporarily destroying this instance of the activity to save space

 

The activity lifecycle.

 

package com.example.android_lifecycle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	private final String TAG = "MainActivity";
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "==onCreate");
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	Log.i(TAG, "==onStart");
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Log.i(TAG, "==onResume");
    }
    
    @Override
    protected void onRestart() {
    	super.onRestart();
    	Log.i(TAG, "==onRestart");
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	Log.i(TAG, "==onPause");
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.i(TAG, "==onStop");
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	Log.i(TAG, "==onDestroy");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

1、首次进入:
08-26 22:09:25.765: I/MainActivity(11029): ==onCreate
08-26 22:09:25.765: I/MainActivity(11029): ==onStart
08-26 22:09:25.765: I/MainActivity(11029): ==onResume

2、返回home:
08-26 22:09:37.480: I/MainActivity(11029): ==onPause
08-26 22:09:37.890: I/MainActivity(11029): ==onStop

3、再次进入:
08-26 22:09:53.910: I/MainActivity(11029): ==onRestart
08-26 22:09:53.910: I/MainActivity(11029): ==onStart
08-26 22:09:53.910: I/MainActivity(11029): ==onResume

4、返回键:
08-26 22:10:08.770: I/MainActivity(11029): ==onPause
08-26 22:10:09.145: I/MainActivity(11029): ==onStop
08-26 22:10:09.145: I/MainActivity(11029): ==onDestroy

 

Saving activity state

implementing an additional callback method that allows you to save information about the state of your activity: onSaveInstanceState().

 

Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient(临时的) state of the activity。you should never use it to store persistent data。

Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.(建议持久化)

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Edexcel GCSE 计算机科学课程活动.doc

    - **Activity 1.3**: - 复制并运行程序 `print("hello " * 10)`,了解字符串乘法的概念。 - 编写程序打印出自己的名字六次。此活动加深了对循环概念的理解。 **第2课时活动** - **Activity 2.1**:判断关于...

    Kotlin 1.3 案例集锦第五章 登录登录Activity初始

    Kotlin 1.3 案例集锦第五章 登录登录Activity初始化

    Kotlin 1.3 案例集锦第五章 登录登录Activity事件

    Kotlin 1.3 案例集锦第五章 登录登录Activity事件绑定处理1

    Ji_Learning_Temporal_Action_Proposals_With_Fewer_Labels_ICCV_201

    通过引入特定于序列数据的扰动技术和Mean-Teacher架构,模型在ActivityNet v1.3和THUMOS14等数据集上的表现与全监督的最先进方法相当甚至更好。这项工作为视频理解和行动检测提供了新的思路,尤其是在大规模视频数据...

    activity切换翻转效果

    在Android应用开发中,Activity是应用程序的基本组件,用于展示用户界面和处理用户交互。当我们希望在不同的Activity之间进行切换时,通常会采用各种动画效果来提升用户体验,"activity切换翻转效果"就是其中一种...

    ActivityNet: A Large-Scale Video Benchmark for Human Activity Understanding大型视频人物活动理解基准数据集ActivityNet-数据集

    ActivityNet is provided by Universidad del Norte, Colombia and King Abdullah University of Science and Technology (KAUST), Saudi Arabia.本数据集由北方大学和阿卜杜拉国王科技大学提供。 activity_...

    opengoo 1.3 RC1

    - usability: Added link to RSS feed for a workspace's recent activity in the dashboard description widget - usability: Monthly view scroll, didn't mantain the toolbar like the others views - ...

    jChat1.3.zip

    3. **AndroidManifest.xml**:描述应用的基本信息,如应用名称、权限、活动(Activity)、服务(Service)等组件的定义。 4. **build.gradle**:构建脚本,定义应用的依赖库、版本信息和构建配置。 5. **gradle-...

    软件研发 Kotlin 1.3 案例集锦第五章 登录登录Ac

    软件研发 Kotlin 1.3 案例集锦第五章 登录登录Activity事件.3gp

    Android Activity和Intent机制学习笔记

    **1.3 状态保存与恢复** 为了应对系统回收内存导致的Activity重建情况,Android提供了`onSaveInstanceState()`和`onRestoreInstanceState()`方法。前者用于保存Activity的状态信息,后者用于恢复这些状态。 - `...

    Robotium自动化测试框架部署文档V1.3

    它允许开发者编写可运行在多个Activity上的黑盒测试,极大地简化了Android应用的UI测试。在本文档中,我们将深入探讨Robotium的部署过程及其核心概念,以便您能有效地利用这一工具进行高效测试。 一、Robotium简介 ...

    Android 开发即时聊天工具 YQ 1.3

    除此之外,Android的Intent机制被用来在不同的活动(Activity)之间进行切换,实现聊天窗口的打开、关闭以及消息通知等功能。 消息推送服务也是必不可少的,考虑到用户可能在不打开应用的情况下接收新消息。Google...

    android ndk 移植的sdl-1.3

    3. **创建Android工程**:使用Android Studio创建一个新的Android项目,选择"Empty Activity"模板。在项目的`jni`目录下,我们将放置SDL源码。 4. **修改SDL源码**:为了适应Android平台,可能需要对SDL源码做一些...

    jChat1.3.rar

    这一层还包括Activity、ContentProvider、BroadcastReceiver和Service等核心组件。 应用层:这是Android设备用户能直接交互的层级,应用程序通过系统API与系统交互。用户可以通过Google Play等平台下载和安装应用。 ...

    Android--开发--jChat1.3.rar

    2. **资源文件**:包括XML布局文件(activity和fragment),图片、图标、字符串资源等。可能还有自定义视图的drawables和颜色资源。 3. **依赖库**:在build.gradle文件中,可以看到应用所依赖的各种第三方库,如...

    TSP:用于定位任务的视频编码器的时间敏感预训练_python_代码_下载

    我们为 ActivityNet v1.3 和 THUMOS14 视频提供了预提取的特征。特征文件以 H5 格式保存,我们将每个特征映射video-name到一个大小为 的特征张量N x 512,其中N是特征数量,512是特征大小。使用h5pypython 包读取...

    android-paysdk1.3-d9lab-DEMO.rar

    本文将详述如何使用"android-paysdk1.3-d9lab-DEMO"这个压缩包来实现在安卓应用程序中集成这两种支付方式,以及相关的知识点。 首先,"android-paysdk1.3-d9lab-DEMO.rar"是一个包含Android支付SDK的示例项目,主要...

    Kotlin Coroutines by Tutorials (Kotlin 1.3)

    1. **在Activity或Fragment中使用协程** 在Android应用中,协程可以帮助我们避免主线程被阻塞。通常在`lifecycleScope`中启动协程,确保当组件销毁时,协程也会被取消。 2. **协程与LiveData** 结合协程和...

    voice_activity_detection:基于深度学习和TensorFlow的语音活动检测

    语音活动检测项目 关键字:Python,TensorFlow,深度学习,时间序列分类 目录 1.11.21.3 2.12.2 5.15.2将5.35.4 去做 资源 1....该项目旨在: ... 1.3 Docker安装 构建docker镜像: $ sudo make build (这可能

Global site tag (gtag.js) - Google Analytics