坚持Android的方向变化的数据的最佳方式方向、方式、数据、Android

2023-09-05 03:25:51 作者:城.

我的教育应用程序有一个选项卡主机6〜7片与横向和纵向模式的支持。在与标签相关联的每一个活动,我显示在列表视图中一些学生记录,一些供选择复选框和一些评价吧,按钮等我通过Web服务调用获取远程服务器上的这些学生记录。出于某些原因,我的应用程序会显示与做一些动作后,一些按钮中的一个初始相对布局/搜索它会显示在结果列表视图中,在单击列表项或按钮,在列表视图中,我表现出一些其他的布局。这适用于所有的活动选项卡的主机。

My education application is having a tab host with 6 to 7 tabs with landscape and portrait modes support. In each and every activity associated with a tab, I am showing some student records in list view, with some check boxes for selection and some rating bars, buttons etc. I am getting these student records from remote server through web service call. For some reasons, my app will show one initial relative layout with some buttons after doing some actions/searches It will show results in list view, upon clicking list item or buttons in list view I am showing some other layout. This applies to all activities in tab host.

如果用户改变方向我必须恢复状态相同或相同的屏幕意味着,如果用户previous看到记录我必须表明的记录列表,如果最初的我,只显示初始列表。我不想逼停的Andr​​oid破坏活动的方向变化或覆盖公共对象onRetainNonConfigurationInstance()。如建议在一些博客和Android Doc的这些可能会导致在以后的操作方面的问题。我所做的仅仅是保存所有必需的信息等,其屏幕1或2或3,文字标签和的onSaveInstanceState 所有的原始数据类型捆绑,并恢复他们在 onRestoreInstanceState 。另外,我写我的自定义搜索结果的对象数组文件,并retriving和的onSaveInstanceState和onRestoreInstanceStates之间将其删除。由于我的搜索结果有100的对象,我存储原始数据类型列表中,这增大堆大小时迅速用户迅速旋转设备的数量巨大。

If user changes orientation I have to restore the same state or same screen means if user previous seeing list of records I have to show list of records, If initial I have to show initial only. I don't want to force stop android destroying activity on orientation change or overriding public Object onRetainNonConfigurationInstance ().As suggested in some blogs and android doc's these may cause problems in later operations. What I did is simply saving all required info like which screen 1 or 2 or 3, Text view labels and all primitive data types in onSaveInstanceState Bundle and restoring them in onRestoreInstanceState. Also I am writing my custom search results Object array to file and retriving and deleting it between onSaveInstanceState and onRestoreInstanceStates. As my search results has 100's of objects and I am storing huge volumes of primitive data type lists, which increasing heap size rapidly when user rapidly rotates the device.

什么是对付它的最好办法

What is the best way to deal with it

1)坚持的onSaveInstanceState和onRestoreInstanceStates,这使我的屏幕空白了一段时间,并增加堆大小,不一定非执行保存,当用户旋转多次恢复

1) persisting in onSaveInstanceState and onRestoreInstanceStates, which makes my screen blank for some time and increasing heap size, un necessarily performing save and restore when user rotating multiple times

2),保持所有所需要的数据,其中一些为静态的,因为它不是本地的活动,也不会破坏

2) keeping all needed data some where as static, as it is not local to activity, it won't destroy

3)通过扩展应用程序类保存所有需要的数据?

3) keeping all needed data by extending Application class?

推荐答案

我会建议你从清单,并通过程序固定取向如果方向配置改变(你可以添加监听器),你可以因为它看起来像方位调整你的意见已经变了。通过这种方式,你可以保存你的数据,以及不必要的存储器。对齐和动态重新定位的看法也不会含糊不用担心。

I will suggest you to fix the orientation from manifest and through program if orientation configuration changes(you can add listener) you can adjust your views as it looks like orientation has been changed. In this way you can save your data as well unnecessary memory storage. Aligning and repositioning views dynamically won't be vague don't worry.

  // Add inside manifest.
  android:configChanges="orientation|keyboardHidden"

  // Reposition your views
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        align_landscape();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        align_portrait();
    }
}