本网页所有文字内容由 imapbox邮箱云存储,邮箱网盘, iurlBox网页地址收藏管理器 下载并得到。
ImapBox 邮箱网盘 工具地址: https://www.imapbox.com/download/ImapBox.5.5.1_Build20141205_CHS_Bit32.exe
PC6下载站地址:PC6下载站分流下载
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox 网页视频 工具地址: https://www.imapbox.com/download/ImovieBox4.7.0_Build20141115_CHS.exe
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
我近期学习了一下android sdk,不太清楚里面的一些原理,如何保存应用程序的状态,看下面hello的小例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.android.hello; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
mTextView = new TextView( this );
if (savedInstanceState == null ) {
mTextView.setText( "Welcome to HelloAndroid!" );
} else {
mTextView.setText( "Welcome back." );
}
setContentView(mTextView);
}
private TextView mTextView = null ; } |
这个android 小例子应该是每个人都开发过的,我每次关闭程序再打开是都是给我显示第一个Welcome to HelloAndroid ,我想在第二次访问的时候能够显示 Welcome back,我该如何处理?
你需要重写 onSaveInstanceState(Bundle savedInstanceState),把你想要保存的状态写入到 Bundle 中,如下
1
2
3
4
5
6
7
8
9
10
11
12
|
@Override public void onSaveInstanceState(Bundle savedInstanceState) {
super .onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean( "MyBoolean" , true );
savedInstanceState.putDouble( "myDouble" , 1.9 );
savedInstanceState.putInt( "MyInt" , 1 );
savedInstanceState.putString( "MyString" , "Welcome back to Android" );
// etc. } |
Bundle 的本质是键值对的方式存储,你可以通过 onCreate()和 onRestoreInstanceState() 或的对应的值
1
2
3
4
5
6
7
8
9
10
|
@Override public void onRestoreInstanceState(Bundle savedInstanceState) {
super .onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean( "MyBoolean" );
double myDouble = savedInstanceState.getDouble( "myDouble" );
int myInt = savedInstanceState.getInt( "MyInt" );
String myString = savedInstanceState.getString( "MyString" ); } |
您通常会使用这项技术来存储实例的值的应用程序(选择,未保存的文本等)。
原文地址:https://www.itmmd.com/201410/38.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。
阅读和此文章类似的: 程序员专区