3

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.

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) 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) keeping all needed data some where as static, as it is not local to activity, it won't destroy

3) keeping all needed data by extending Application class?

Ganesh K
  • 2,623
  • 9
  • 51
  • 78

2 Answers2

2

You can also use fragments which can be retained through orientation changes.

Gautham
  • 3,418
  • 3
  • 30
  • 41
1

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();
    }
}
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • @Arflin I am suspecting force stopping activity destroy like above may cause problems especially when you are dealing with locale or multiple screens. – Ganesh K Jul 20 '12 at 12:07
  • I had the same issue long before and fixed with same way. Here view means what ever view you are showing on the screen, like buttons, textview, listview etc ... screen will be single one only. – Daud Arfin Jul 20 '12 at 12:12
  • but my view state will change dynamically based on user actions for ex for first btn click btn background may be blue, for 2nd click it is red for third it may be some other color etc. So i have to track all those changes. If it is single list view with a set of data, then u r solution is absolutely fine. – Ganesh K Jul 20 '12 at 12:17
  • Yes you need to track, you can easily maintain one flag for your button state. Hopes better and easy compare to other solutions.. – Daud Arfin Jul 20 '12 at 12:22