0

I am trying to implement orientation feature in the app. The app is basically made of tabs, using TabHost. Each tab is an activity group with a few activities. Each of this child activity runs a background task using AsyncTask to pull out server data and render the UI.

For instance,

HomeActivity- tab activity class that creates all the tabs
UserActivity- an activity group class representing a tab (say Tab 1)
TabOne- child activity inside UserActivity group

User sees Tab 1 soon after successful login. Issue begins when I tried to set orientation capabilities to this tab. Within the activity (TabOne), I added the following

@Override
public void onSaveInstanceState(Bundle outState) {
  System.out.println("saving state..!");
  outState.putStringArray("imageUrls", imageArray);
  super.onSaveInstanceState(outState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  System.out.println("restoring state..!");
  imageArray = savedInstanceState.getStringArray("imageUrls");
  super.onRestoreInstanceState(savedInstanceState);

}

I am able to obtain 'imageUrls' state. But, later down, I could not access the static tab host variable.

 HomeActivity.tabHost.getTabWidget().getChildAt(1)

tabHost variable is null! I am not saving any of its state. How could I possibly save tabHost instance?

While researching other community posts, I found 'onRetainNonConfigurationInstance()'. I implemented the same.

 public Object onRetainNonConfigurationInstance() {
   System.out.println("retaining state..!");
   return HomeActivity.tabHost;
 }

Bu this object is returned as null. Can anyone walk me through steps to handle orientation changes in apps with tabHost?

Ask me if you find anything confusing.

Thanks in advance!

SOLVED: Adding screenSize to activity tag in the manifest fixed the issue.

<activity android:name=".HomeActivity"
          android:configChanges="orientation|screenSize"/>
Renjith
  • 3,457
  • 5
  • 46
  • 67

1 Answers1

0

You can add the parameter

android:configChanges="orientation"

to your activity in manifest. I think it does what you want. It should look like:

<activity
        android:name="com.your.package.YourActivity"
        android:configChanges="orientation"
</activity>
azertiti
  • 3,150
  • 17
  • 19
  • already added this. android:configChanges="keyboard|keyboardHidden|orientation" – Renjith Dec 20 '12 at 16:42
  • Did you add it both to the activity having the TabHost and also to the activities displayed in tabs? – azertiti Dec 20 '12 at 16:46
  • I can see that screen orientation changes in the emulator. Soon after, NullPointerException is thrown at tabHost variable. – Renjith Dec 20 '12 at 16:54
  • Congrats, can you also post the solution ? :) – azertiti Dec 21 '12 at 14:30
  • Edited the question! check it out ;) – Renjith Dec 21 '12 at 14:49
  • I think you have been unlucky with the device you have. Earlier today I used the exact same solution for a phone that was rotating the screen even if the app requested only "portrait" mode. Of course, it was fine on all the others I tried without the "screenSize". – azertiti Dec 21 '12 at 15:31