简体   繁体   English

Android onsaveinstancestate()

[英]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.

相关问题 Android按钮的onSaveInstanceState() - onSaveInstanceState() for Android Buttons Android onSaveInstanceState始终为null - Android onSaveInstanceState is always null Java-Android SDK-savedInstanceState和onSaveInstanceState - Java - Android SDK - savedInstanceState & onSaveInstanceState Android onSaveInstanceState没有获取类变量 - Android onSaveInstanceState not getting class variables 在Android中通过onCreate使用onSaveInstanceState检索数据 - Retrieving data with onSaveInstanceState via onCreate in Android AsyncStorage 和 OnSaveInstanceState android 之间有什么区别 - what is the difference between AsyncStorage and OnSaveInstanceState android Android编程使用onsaveinstancestate保存数组 - android programming saving array using onsaveinstancestate Android PreferenceActivity:onSaveInstanceState之后,onCreate方法中的bundle为空 - Android PreferenceActivity : Bundle is null in onCreate method after the onSaveInstanceState Android Notepadv3教程-是否真的需要onSaveInstanceState中的saveState()? - Android Notepadv3 tutorial - Is saveState() in onSaveInstanceState really necessary? 如何在 Android Studio 中从 Onclick() 到 onSaveInstanceState() 获取字符串值 - How to get string value from Onclick() to onSaveInstanceState() in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM