Android 报错 Activity 状态
处理方法
你需要重写 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" ); } |
您通常会使用这项技术来存储实例的值的应用程序(选择,未保存的文本等)。
原文地址:http://www.itmmd.com/201410/38.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。