简体   繁体   English

方向改变时改变活动

[英]Change activity when orientation changes

In my app I have MainActivty which should always be viewed in protrait. 在我的应用程序中,我具有MainActivty,应始终按主视图进行查看。 If I turn the device to landscape I switch to TrendsActivity (to show some graphs). 如果将设备转到横向,则切换到TrendsActivity(以显示一些图形)。 This is done by the folliwing code in MainActivty: 这是通过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: 然后,当我切换回肖像时,我只需在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. 这样就完成了活动,并自动移回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. 但是,如果我在TrendsActivity中按下返回按钮,则会以横向模式返回MainActivity,但我不希望那样。 I tried to call the following code in MainActivity: 我试图在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... 这是行不通的,因为现在如果从不调用MainActivity的onConfigurationChanged()……

What to do? 该怎么办?

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? 您是否不认为通过简单地为横向模式指定备用xml并让MainActivity相应地工作,可能会实现更简洁的设计? Even without this, launching another activity on orientation change isn't going to be what most users would expect. 即使没有这些,大多数用户也不会期望启动另一个有关方向改变的活动。

use this in 用这个

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

in onResume() of MainActivty 在MainActivty的onResume()中

You can try to set your orientation in android manifest only, try below code 您可以尝试仅在android清单中设置方向,请尝试以下代码

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 : 您应该将此添加到AndroidManifest

android:configChanges="orientation"

this way you tell Android to call onConfigurationChanged when there is a change in the orientation. 这样,您可以告诉Android在方向更改时调用onConfigurationChanged

I solved this by changing the code in my MainActivity to just change the layout XML like fremmedehenvendelser suggested (Tusen takk!) 我通过更改MainActivity中的代码来解决此问题,只需更改布局XML,如fremmedehenvendelser建议(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 被注释掉的代码不起作用...在清单上添加TrendsActivity之前

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM