Android 简单案例:onSaveInstanceState 和 onRestoreInstanceState
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; public final class MultiRes extends Activity { private int mCurrentPhotoIndex = 0;
private int[] mPhotoIds = new int[] { R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7 }; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); showPhoto(mCurrentPhotoIndex); // Handle clicks on the 'Next' button.
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCurrentPhotoIndex = (mCurrentPhotoIndex + 1)
% mPhotoIds.length;
showPhoto(mCurrentPhotoIndex);
}
});
} @Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("photo_index", mCurrentPhotoIndex);
super.onSaveInstanceState(outState);
} @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
mCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
showPhoto(mCurrentPhotoIndex);
super.onRestoreInstanceState(savedInstanceState);
} private void showPhoto(int photoIndex) {
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(mPhotoIds[photoIndex]); TextView statusText = (TextView) findViewById(R.id.status_text);
statusText.setText(String.format("%d/%d", photoIndex + 1,
mPhotoIds.length));
}
}
Android 简单案例:onSaveInstanceState 和 onRestoreInstanceState的更多相关文章
- Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:
Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...
- 保存恢复临时信-Android 中使用onSaveInstanceState和onRestoreInstanceState
在Activity中,有两个方法用于临时保存.恢复状态信息,这两个方法是: public void onSaveInstanceState(Bundle savedInstanceState); pu ...
- Android 简单案例:可移动的View
CrossCompatibility.rar 1. VersionedGestureDetector.java import android.content.Context; import andro ...
- Android 简单案例:继承BaseAdapter实现Adapter
import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import ...
- Android 中onSaveInstanceState和onRestoreInstanceState学习
1. 基本作用: Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPaus ...
- Android onSaveInstanceState和onRestoreInstanceState()
首先来介绍onSaveInstanceState() 和 onRestoreInstanceState() .关于这两个方法,一些朋友可能在Android开发过程中很少用到,但在有时候掌握其用法会帮我 ...
- Android学习基础之onSaveInstanceState和onRestoreInstanceState触发的时机
先看Application Fundamentals上的一段话: Android calls onSaveInstanceState() before the activity becomes ...
- android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】
android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...
- Android Activity生命周期 onSaveInstanceState和onRestoreInstanceState
当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候. 注意上面 ...
随机推荐
- 六步破解win2008R2登录密码
防火墙没有开启,win2008R被当成矿机,只好重新破解密码进去解决问题,试了好多方法,下列方法绝对实用简单. 破解2008登录密码的方法: 1.进入PE2.找到文件:windows\system32 ...
- ATM交换机 和普通交换机区别
运行在 ATM协议上的交换机 普通的是运行在 以太网协议上的 ATM交换机 转发的是广域网二层协议数据包,以太网交换机转发的是局域网二层协议数据包. 网络的ATM是指:异步传输模式,全称是什么 Asy ...
- 怎样在IIS下配置PHP
首先下载Windows的PHP安装包.随后将该包解压至C:\PHP.完成上面的步骤后,将C:\php目录下的php.ini-dist文件改名为php.ini,然后拷到C:\Windows目录下. 用记 ...
- Python 爬验证码
主要实现功能: - 登陆网页 - 动态等待网页载入 - 验证码下载 非常早就有一个想法,就是自己主动依照脚本运行一个功能.节省大量的人力--个人比較懒.花了几天写了写,本着想完成验证码的识别,从根本上 ...
- CDH离线安装
1. 安装准备 系统:Centos 6 Cloudera Manager分配如下: 安装版本:CDH-5.8.0 所需安装文件 CDH相关 CDH-5.8.0-1.cdh5.8.0.p0.42-el6 ...
- r函数知识总结
1. rbind(), cbind(): 构造.合并vector 或matrix为一个矩阵:cbind(1, 1:10) ----默认列合并, rbind(1, 1:10) ----行合并(or构造 ...
- C语言 float、double数据在内存中的存储方式
float在内存中占4个字节(32bit),32bit=符号位(1bit)+指数位(8bit)+底数位(23bit) 指数部分 指数位占8bit,可以表示数值的范围是0-(表示0~255一共256个数 ...
- Python3.4下使用sqlalchemy
一. 1.用sudo apt-get install python3-numpy之后,会默认把numpy安装到 /usr/lib/python3/dist-packages目录下,而且版本比较低. ...
- Eclipse/MyEclipse全屏插件
此插件可以让Eclipse/MyEclipse的界面全屏,隐藏菜单栏和状态栏! MyEclipse 2014/2015中亲测有效! 插件下载: http://files.cnblogs.com/got ...
- Android检测Cursor泄漏的原理以及使用方法(转)
简介: 本文介绍如何在 Android 检测 Cursor 泄漏的原理以及使用方法,还指出几种常见的出错示例.有一些泄漏在代码中难以察觉,但程序长时间运行后必然会出现异常.同时该方法同样适合于其他需要 ...