2013-06-19 14 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)); 
     } 
    }