简体   繁体   English

onSaveInstanceState在方向更改时被调用两次

[英]onSaveInstanceState gets called twice on Orientation Change

I am working on a Android application where in I want to save the information entered into the textviews of the activity in the database whenever the phone call arrives, orientation of the app is changed, etc. 我正在使用一个Android应用程序,在该应用程序中,我想在每次电话到达,更改应用程序方向等情况下将输入到活动文本视图中的信息保存到数据库中。

I am using this code to save the information entered into the textview by the user. 我正在使用此代码保存用户输入到textview中的信息。

@Override
protected void onSaveInstanceState(Bundle outState) {
        save();
        Toast.makeText(getApplicationContext(), "On save state", Toast.LENGTH_SHORT).show();
    super.onSaveInstanceState(outState);
}

Problem : 问题:

Whenever my phone orientation is changed the onSaveInstanceState method is called twice. 每当更改我的电话方向时,都会两次调用onSaveInstanceState方法。 Can anyone explain me the reason for this. 谁能解释我的原因。

Because it is called twice, save() also get called twice and I have duplicate entries into my database table - which is not good. 因为它被调用了两次,所以save()也被调用了两次,并且我的数据库表中有重复的条目-不好。

I cannot use android:configChanges="orientation|screenSize" in manifest file to deal with orientation change. 我无法在清单文件中使用android:configChanges="orientation|screenSize"处理方向更改。 As it makes the ads disappear when user changes landscape to portrait. 当用户将横向更改为纵向时,这会使广告消失。

onSaveInstanceState is called only once when phone call is received :) 仅在收到电话时才调用onSaveInstanceState :)

Note : save() is also called onBackPressed() method 注意:save()也称为onBackPressed()方法

Please assist ! 请协助 !

First, make sure that what you really want to do is saving persistent data in a database. 首先,请确保您真正想要做的是将持久性数据保存在数据库中。 If so, onSaveInstanceState is not your method. 如果是这样,则onSaveInstanceState不是您的方法。

onSaveInstanceState is used to save the temporary state of the activity to be able to recreate it exactly as it was on the onCreate or onRestoreInstanceState . onSaveInstanceState用于保存活动的临时状态,以便能够像在onCreateonRestoreInstanceState上一样完全重新创建活动。 The default implementation already saves/restores most of the UI standard views and if you override it probably you will want to keep calling the default implementation and then save/restore the additional values. 默认实现已经保存/恢复了大多数UI标准视图,如果您覆盖它,则可能需要继续调用默认实现,然后保存/恢复其他值。 By the way, the values are stored on the Bundle which basically provides a way to store/retrieve key-value pairs. 顺便说一下,这些值存储在Bundle中 ,该Bundle基本上提供了一种存储/检索键值对的方法。

If you really want to save persistent state, you should do it on the onPause method that is part of the lifecycle of the Activity . 如果您确实要保存持久状态,则应在Activity生命周期中的onPause方法上进行操作。 Note what the activity documentation says: 请注意活动文档的内容

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation. 请注意,将持久性数据保存在onPause()中而不是onSaveInstanceState(Bundle)中非常重要,因为后者不是生命周期回调的一部分,因此不会在其文档中描述的每种情况下都调用它。

To avoid the problem with duplicate rows, you just need to check if the record you are trying to save already exists or not (you need some kind of unique identifier for this) and then create or update accordingly. 为了避免重复行的问题,您只需要检查要保存的记录是否已经存在(为此需要某种唯一标识符),然后相应地创建或更新。

In my case, it was getting called twice when returning from camera intent + orientation change . 就我而言,从相机意图+方向更改返回时,它被调用了两次。 Then i ended up writing Singleton Manager class to save and restore my variables whenever i want. 然后,我最终编写了Singleton Manager类来随时保存和还原变量。 This class holds your variables in static final variables and you should clear it when you no longer use it. 此类将变量保存在static final变量中,并且在不再使用它时应清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM