Question

In my app I have MainActivty which should always be viewed in protrait. If I turn the device to landscape I switch to TrendsActivity (to show some graphs). This is done by the folliwing code in MainActivty:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Intent intent = new Intent(this, TrendsActivity.class);
        startActivity(intent);
    } 
}

Then when I switch back to portait i simply call this in TrendsActivity:

    @Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        finish();
    }
}

This finishes the activity an automaticly moves back to MainActivity. So far so good. But if I press the back button while in TrendsActivity I come back to MainActivity in landscape mode, and I don't want that. I tried to call the following code in MainActivity:

    @Override
protected void onRestart()
{
    super.onRestart();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}

This does not work, because now the onConfigurationChanged() if MainActivity is never called...

What to do?

Was it helpful?

Solution

Don't you think you might be achieving a much cleaner design by simply specifying an alternate xml for landscape mode and letting your MainActivity behave accordingly? Even without this, launching another activity on orientation change isn't going to be what most users would expect.

OTHER TIPS

use this in

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

in onResume() of MainActivty

You can try to set your orientation in android manifest only, try below code

android:screenOrientation="portrait"

so exactly your code in manifest looks something like

<activity
   android:name=".yourActivityName"
   android:screenOrientation="portrait"
</activity>

You should add this to the AndroidManifest:

android:configChanges="orientation"

this way you tell Android to call onConfigurationChanged when there is a change in the orientation.

I solved this by changing the code in my MainActivity to just change the layout XML like fremmedehenvendelser suggested (Tusen takk!)

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        //setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
        setContentView(R.layout.activity_trends);
    }
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        setContentView(R.layout.activity_main);
    } 
}

Next problem is to set the theme to make this layout show in fullscreen. The code which is commented out does not work... Before I had this in my manifest for TrendsActivity

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top