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键的时候. 注意上面 ...
随机推荐
- uboot中log处理
位图或logo和开机显示画面,是两个完全不同的东西. logo显示uboot相关信息,如版本号等. 开机画面是用户下载到固定位置后uboot加载的. 1.开机画面 在uboot中使用splash sc ...
- Tomcat:基础安装与使用教程
Tomcat:基础安装与使用教程 背景 此文记录了 Tomcat 的基本使用方法,主要为了强化记忆. 安装步骤 第一步:下载和安装 Java 下载地址:http://www.oracle.com/te ...
- 用route命令解决多出口的问题
网络已经走进了我们的生活.工作.学习之中,大多数单位.公司都已经连接到了Internet.但是,因为各种原因,有这样一个问题存在.就是:这些单位即有到公网(Internet)的出口连接,也有到专网(单 ...
- Hadoop集群作业调度算法
转自:http://blog.csdn.net/chen_jp/article/details/7983076 Hadoop集群中有三种作业调度算法,分别为FIFO,公平调度算法和计算能力调度算法 先 ...
- Ubuntu adb devices : no permissions 解决方法
ntun下USB连接Android手机后,使用adb devices 出现如下: List of devices attached ???????????? no permissions 同时在DDM ...
- html学习笔记五
关于服务端和client的校验问题 上述的表格信息填写后发现,即使有些信息不添,依旧能够提交 所以针对此问题,我们要在client进行数据填写信息的增强型校验(必添单元,必须填写有效信息,否则无法提交 ...
- 如何研究某个gene的ceRNA 网络
研究人员针对 PTEN 这个关键的抑癌基因,来探究调控该基因表达的ceRNA 网络: 分析策略: 1)预测能调控该基因的miRNAs 通过miRanda 软件预测和实验验证相结合的方式,挑选出 miR ...
- Asp.net 程序优化js,css合并与压缩
访问时将js和css压缩并且缓存在客户端,采用的是Yahoo.Yui.Compressor组件还完成的,从这里可下载 创建一个IHttpHandler来处理文件 ) }; ) ...
- CMD命令进入文件夹
cmd 进入E文件夹 E: 查看文件夹目录 dir 进入某个文件夹 cd 目录
- stylus入门使用方法
https://segmentfault.com/a/1190000002712872