2013-06-19 15 views
0

我希望在定向为此我使用从potrait景观变化加载新的活动:开始在方向变化的新活动

@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     startActivity(new Intent(this, ABCDActivity.class)); 
    } 

    if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     super.onConfigurationChanged(newConfig); 
     finish(); 
    } 
} 

但它不与方向的改变而改变我的活动。我错过了什么吗?

+0

请参阅此链接 - http://stackoverflow.com/questions/12983792/how-to-switch-to-other-activity-at-orientation-change-and-then-back-again – NarendraJi

回答

1

的onConfigurationChanged()方法将不会被调用,除非你宣布在AndroidManifest.xml下各自的属性:

android:configChanges="orientation" 
0

为什么你想在你的方向改变时开始一个新的活动? 最好为不同的配置提供不同的资源!或者在1 Activity中使用Fragments。

更多信息可以在这里找到:

编辑: 您只需创建,再现本身的方向改变一个活动。因此,请删除您在AndroidManifest.xml中的任何configChanges="screenSize..."。 在活动的xml文件中,您可以链接到另一个片段。 同样,有关这方面的更多信息可以在上面的链接中找到。

MyActivity.java

public class MyActivity extends Activity 
{ 
@Override 
public void onCreate(Bundle cycle) 
{ 
    super.onCreate(cycle); 
    this.setContentView(R.layout.myactivity); 
} 
} 

RES /布局/ myactivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     class="com.example.myapp.fragments.TimeFrameGraphFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</FrameLayout> 

而RES /布局,土地/ myactivity.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     class="com.example.myapp.fragments.AllDataGraphFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</FrameLayout> 
+0

我显示数据(在一个图表中)在纵向模式下的特定时间间隔,但我想在用户切换到横向时显示所有数据。 –

+0

听起来像是使用布局/布局/文件夹/文件夹的内置资源系统的完美工作。我用一些示例代码更新了我的答案。如果它帮助你,请接受它。 – Jelle

1

在你ManifestActivity标签对于您希望进行配置更改的每项活动,您必须放置以下行:

机器人:configChanges = “方向|屏幕尺寸”

作为一个应用程序设计笔记......开始,不建议在方向变化的新活动......

2

试试这个,

@Override 
    public void onConfigurationChanged(Configuration newConfig) 
    { 
    super.onConfigurationChanged(newConfig); 
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
     { 
     startActivity(new Intent(this, ABCDActivity.class)); 
     } 
    }