3

I have a TabActivity that hosts other three activities.

If the first tab is selected, I can change the device orientation and everything is ok.

If I go to the second or the third tab and change the orientation, the tab sub activity's(second and third) onCreate() method gets called twice, for the first time is setting the default tab content(the first one) and at the second onCreate, is setting the selected tab content(as it should for the first time).

Why is onCreate() method being called twice and how can I solve the problem?

EDIT:

I don't want to use android:configChanges="orientation" in the manifest because I want to get the app title and system bar down and on portrait to show them, and this is possible only before setting a content...

Adinia
  • 3,722
  • 5
  • 40
  • 58
Cata
  • 11,133
  • 11
  • 65
  • 86

3 Answers3

1

use the android:configChanges="orientation" attribute in manifest file like below

<activity android:name=".Settings" android:screenOrientation="portrait"
            android:configChanges="orientation"></activity>
Pinki
  • 21,723
  • 16
  • 55
  • 88
  • Hello, I forgot to write that I don't want to use configChanges, please read under EDIT in the post. thank you for trying to help aniway. – Cata Nov 04 '11 at 13:40
  • why can't you use config changes? if you add config changes to the tab activity for orientation. do not add the screenOrientation to your xml so that it can rotate if your xml is setup properly then the tabs will rotate to landscape. In your activity you can use onConfigurationChange to switch your layout if you need to. I can add an answer with what i mean as well. – caguilar187 Nov 13 '11 at 05:24
  • My app is changing the rotation.. I don't have screenOrientation on my manifest file. – Cata Nov 13 '11 at 20:57
1

Just so that I am clear, are you saying that you have several tabs, which are using different orientations (portrait or landscape), and you are having issues when switching tabs and setting the corresponding orientations properly?

In response to Cata's comment:

Yes, so anytime you rotate the screen the currently viewable activity is destroyed and onCreate is called again (if I recall the steps). What you have to do is call getCurrentTab(), which returns the int value representing the tab, and resetting that as the active tab when onCreate is called. You can do this in several ways...either by having a small method that handles just that and calling it via onCreate, or by using onSaveInstanceState(Bundle) to save your current data, and onRestoreInstanceState() to reload your tab data.

You can set a global int (int currentTab = 0), not setting it in onCreate(), and in your onSaveInstanceState(Bundle) method you can save that to the current tab (currentTab = getCurrentTab()), then in onRestoreInstanceState() you can set it again.

Does that make sense?

Keep in mind that I have not tested that, but willing to do so if you aren't familiar with those two method calls.

Below is an example of saving data to the Bundle - also recall that onCreate accepts that activity bundle as a parameter.

@Override
public void onSaveInstanceState(Bundle outState){
    // Store UI state to the savedInstanceState.
    // This bundle will be passed to onCreate on next call.
    super.onSaveInstanceState(outState);
    String strMinSec = timer.getText().toString();
    String strMs = timerMs.getText().toString();
    long curElapstedTime = elapsedTime;
    boolean timerStopped = stopped;
    int orientation = this.getResources().getConfiguration().orientation;

    outState.putString("MinSec", strMinSec);
    outState.putString("Ms", strMs);
    outState.putLong("Elapsed", elapsedTime);
    outState.putBoolean("Stopped", timerStopped);
    outState.putInt("Orientation", orientation);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){

    // Restore UI state from the savedInstanceState.
    if (savedInstanceState != null){
        String MinSec = savedInstanceState.getString("MinSec");
        if (MinSec != null)
        {
          timer.setText(MinSec);
        }
        String Ms = savedInstanceState.getString("Ms");
        if (Ms != null)
        {
          timerMs.setText(Ms);
        }
        long elapsed = savedInstanceState.getLong("Elapsed");
        if(elapsed > 0)
            elapsedTime = elapsed;
        int theOrientation = savedInstanceState.getInt("Orientation");
        //if(theOrientation > 0)
            //this.setRequestedOrientation(theOrientation); 
    }
}
RonTron
  • 50
  • 5
  • I have 4 tabs that are using the same orientation.. but if the user wants to switch to landscape he can simply rotate the phone and the rotation is managed by android.. So I want my tab activity to be compatible with both orientations, it seems that the problem comes when I select any tab but the first one and then I rotate the device, the tab activity gets killed and after is recreating is setting the first tab as the selected tab and right after that it kills the first tab and make visible the real selected tab. I hope now it's more clear :) – Cata Nov 12 '11 at 13:00
  • Hmm, thank you for trying to help me. The problem is that I know how to get the selected tab, what I don't know is how can I change the tab from the tabActivity without letting the TabHost to set first the first tab and after that my selected tab... if I try to call setCurrentTab(2) for example, the tabactivity first calls the first's tab onCreate() method and only after that is calling the selected tab's onCreate() method... – Cata Nov 13 '11 at 16:24
0

I think the best solution is to take a different approach by using a single Activity and put every tab content in a different LinearLayout and in onClick() methods of the tabs(which will be some buttons) to switch between the layouts...

If anybody have a better idea, please post it here!

Thank you!

Cata
  • 11,133
  • 11
  • 65
  • 86