Android 2.2 API Demos -- PersistentState(Activity)

2024-03-25 02:18

本文主要是介绍Android 2.2 API Demos -- PersistentState(Activity),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先看API Demo中的示例,PersistentState Activity。

1. 在PersistentState Activity运行窗口的第一个文本框中追加输入"It will be saved.",在第二个文本框中输入"It will be lost."并且光标选择"be saved."。

2. 点击back按钮或者以其它方式退出这个界面。

3. 当再次进入这个界面时发现,在第一个文本框中添加的文字和对文字的选择没有改变,而第二个文本框中新输入的文字消失了。

从上面的示例可以看出当我们离开这个Activity之后,有些信息被保存了。当我们回到这个Activity时,被保存的信息能再次加载进来。

从代码中可以看出,

Java代码

package com.example.android.apis.app;import com.example.android.apis.R;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;public class PersistentState extends Activity
{@Overrideprotected void onCreate(Bundle savedInstanceState) {// Be sure to call the super class.super.onCreate(savedInstanceState);setContentView(R.layout.save_restore_state);// Set message to be appropriate for this screen.((TextView)findViewById(R.id.msg)).setText(R.string.persistent_msg);// Retrieve the EditText widget whose state we will save.mSaved = (EditText)findViewById(R.id.saved);}@Overrideprotected void onResume() {super.onResume();SharedPreferences prefs = getPreferences(0); String restoredText = prefs.getString("text", null);if (restoredText != null) {mSaved.setText(restoredText, TextView.BufferType.EDITABLE);int selectionStart = prefs.getInt("selection-start", -1);int selectionEnd = prefs.getInt("selection-end", -1);if (selectionStart != -1 && selectionEnd != -1) {mSaved.setSelection(selectionStart, selectionEnd);}}}@Overrideprotected void onPause() {super.onPause();SharedPreferences.Editor editor = getPreferences(0).edit();editor.putString("text", mSaved.getText().toString());editor.putInt("selection-start", mSaved.getSelectionStart());editor.putInt("selection-end", mSaved.getSelectionEnd());editor.commit();}private EditText mSaved;
}


当我们点击back按钮离开PersistentState Activity时,触发了onPause()方法,在onPause()方法里我们对第一个文本框的状态(其中的文本和对文本的选择)进行了保存。

当我们再次进入这个Activity时,触发了onResume()方法,在onResume()里我们将保存的信息重新加载到第一个文本框中。

通过onPause和onResume方法,我们可以对Activity的状态改变进行相应,执行一定的操作。其实,Android中Activity的状态改变不止create,pause和resume,相应的方法也不止onCreate,onPause和onResume。

在Android系统中,Activity是通过activity stack来管理的。当一个新的activity被创建,它就被置于栈顶并处于running状态。

Activity有四种状态:

1.处于屏幕最前端的Activity(栈顶)为active或者running状态;
2.失去了焦点但是可见的Activity为paused状态,例如被透明或者非全屏Activity遮盖;
3.被完全掩盖的Activity为stopped状态;
4.当Activity处于paused或者stopped状态时由于系统需要内存被杀死或者执行了finish()方法的Activity将处于destroyed状态。

参考Activity的生命周期图,

相关方法描述如下:

MethodDescriptionKillable?Next
onCreate()Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.Always followed by onStart().NoonStart()
 onRestart()Called after your activity has been stopped, prior to it being started again.Always followed by onStart()NoonStart()
onStart()Called when the activity is becoming visible to the user.Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.NoonResume() or onStop()
 onResume()Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.Always followed by onPause().NoonPause()
onPause()Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.YesonResume() or
onStop()
onStop()Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.YesonRestart() or
onDestroy()
onDestroy()The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.Yesnothing

 

 

 

 

 

这篇关于Android 2.2 API Demos -- PersistentState(Activity)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/843549

相关文章

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

一文详解如何在Vue3中封装API请求

《一文详解如何在Vue3中封装API请求》在现代前端开发中,API请求是不可避免的一部分,尤其是与后端交互时,下面我们来看看如何在Vue3项目中封装API请求,让你在实现功能时更加高效吧... 目录为什么要封装API请求1. vue 3项目结构2. 安装axIOS3. 创建API封装模块4. 封装API请求

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整