简体   繁体   English

防止由于方向更改而导致android状态更改

[英]Preventing state change in android due to orientation change

I use an SQLiteDatabase in my application which I created manually and then using it in the android application. 我在手动创建的应用程序中使用SQLiteDatabase,然后在android应用程序中使用它。 To navigate through it I am using a Cursor. 要浏览它,我正在使用游标。 I am showing the results from rows in edit texts. 我在编辑文本中显示行的结果。

When the orientation changes, irrespective of the row which was shown, the first row is shown in the edit Text. 当方向改变时,无论显示的行是什么,第一行都会显示在编辑文本中。 I know that android recreates the activity on orientation change and calls onCreate() method again. 我知道android会在方向更改时重新创建活动,然后再次调用onCreate()方法。

But according to the documentation , I shouldn't store a Cursor Object in onRetainNonConfigurationInstance() since it uses a Context. 但是根据文档 ,我不应该将游标对象存储在onRetainNonConfigurationInstance()中,因为它使用了上下文。

Here is the code for creating Cursor. 这是创建游标的代码。

DataBaseHelper myDbHelper = new DataBaseHelper(this);
Cursor c=myDbHelper.getDatabase().rawQuery("SELECT * FROM mytable",null);

Another recommended way is to handle the changes manually, but I do not want to do so since I want to take advantage of the default adjustment of UI. 推荐的另一种方法是手动处理更改,但是我不想这样做,因为我想利用UI的默认调整。

How can I continue with the same row when the orientation changes ? 方向改变时,如何继续同一行?

最简单的方法之一是将其添加到清单文件中(用于相关活动)-

android:configChanges="orientation|screenSize|keyboardHidden"

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

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