[英]Android onsaveinstancestate()
so i get the main idea of how to use 所以我得到了如何使用的主要思想
protected void onSaveInstanceState (Bundle outState)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
also from Saving Android Activity state using Save Instance State 也来自使用“保存实例状态”保存Android活动状态
but my problem is what if this was the first time the application is being created? 但是我的问题是,如果这是第一次创建应用程序,该怎么办? then nothing would have been stored in the bundle before....and if so then when i try to call something out from the bundle that hasn't been saved before what do i get?null?
那么以前什么也不会存储在捆绑软件中....如果是的话,那么当我尝试从捆绑软件中调出尚未保存的东西之前,我得到什么呢? for example i have this in my code
例如我的代码中有这个
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String [] b=savedInstanceState.getStringArray("MyArray");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
String [] a={"haha"};
savedInstanceState.putStringArray("MyArray", a);
}
in the FIRST time the application is ever opened what would the value of b be? 第一次打开应用程序时,b的值是什么? and after the application has been used once what would the value of b be?
在应用程序使用一次之后,b的值是多少?
Many thanks! 非常感谢!
in your onCreate() add a condition 在您的onCreate()中添加一个条件
if(savedInstanceState==null){
//meaning no data has been saved yet or this is your first time to run the activity. Most likely you initialize data here.
}else{
String [] b=savedInstanceState.getStringArray("MyArray");
}
by the way to retrieve data that was saved in your onSaveInstanceState you will override this 通过检索保存在onSaveInstanceState中的数据的方式,您将覆盖它
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
You have to check for null always in the onCreate() or in onRestoreInstanceState() like below: 您必须始终在onCreate()或onRestoreInstanceStateState()中始终检查null,如下所示:
String [] b = new String[arraysize];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
b = savedInstanceState.getStringArray("MyArray");
// Do here for resetting your values which means state before the changes occured.
}
else{
default..
}
Here you do general things.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.