Domanda

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...

È stato utile?

Soluzione 3

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!

Altri suggerimenti

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

<activity android:name=".Settings" android:screenOrientation="portrait"
            android:configChanges="orientation"></activity>

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); 
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top