0

我有一个活动,我想根据方向动态添加我的片段,所以我做了以下事情: Calendrier.java

package com.thinline.dm21.calendrier;

public class Calendrier extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendrier_main);
    //ajouter les fragments Planifications et Calendrier mensuel 
    ajouterFragments();       
}

public void ajouterFragments(){
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    if(getResources().getConfiguration().orientation == 1){

    }
    else if(getResources().getConfiguration().orientation == 2){
        ft.add(R.id.calendrier_planifictions, new Planifications());
        ft.add(R.id.calendrier_calendrierhebdomadaire, new CalendrierHebdomadaire());
    }

    ft.commit();
}
}

这是我的布局/calendrier_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendrier_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<LinearLayout
    android:id="@+id/calendrier_planifictions"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

</LinearLayout>

<RelativeLayout
    android:id="@+id/calendrier_calendrierhebdomadaire"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" >
</RelativeLayout>

</LinearLayout>

和我的布局端口/calendrier.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendrier_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<LinearLayout
    android:id="@+id/calendrier_planifictions"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

</LinearLayout>

<RelativeLayout
    android:id="@+id/calendrier_calendrierhebdomadaire"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="2" >
</RelativeLayout>

</LinearLayout>

我的问题是:

  • 当我以纵向模式启动我的应用程序时,我得到一个黑屏,当我旋转它时,我得到我的另外两个片段,这很好,但是当我再次旋转它(纵向)时,我仍然有我的片段而不是我的黑屏, 通常它应该显示一个黑屏,因为 onCreate 和 onDestroy 都被执行了
4

2 回答 2

0

当您旋转时,Android 会保留添加到您的活动中的片段。如果您已经添加了片段并在活动创建时将其删除,则需要标记。

于 2013-07-08T15:33:41.403 回答
0

Android 会在旋转更改后保留片段,因此在纵向模式下,您需要检查是否添加了片段,如果是,则需要将其删除。您的代码应类似于以下示例:

private static final String PLANIFICATIONS_FRAG_TAG = "PLANIFICATIONS_FRAG_TAG";
private static final String CALENDAR_FRAG_TAG = "CALENDAR_FRAG_TAG"; 

public void ajouterFragments(){
    Configuration configuration = getResources().getConfiguration();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();

    Planifications planificationsFrag = fragmentManager.findFragmentByTag(PLANIFICATIONS_FRAG_TAG);
    CalendrierHebdomadaire calendarFrag = fragmentManager.findFragmentByTag(CALENDAR_FRAG_TAG);

    if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
        if (planificationsFrag != null) 
            ft.remove(fragment);
        if (calendarFrag != null) 
            ft.remove(calendarFrag);
     }
     else if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         if (planificationsFrag == null) 
            ft.add(R.id.calendrier_planifictions, new Planifications(), PLANIFICATIONS_FRAG_TAG);
         if (calendarFrag == null) 
             ft.add(R.id.calendrier_calendrierhebdomadaire, new CalendrierHebdomadaire(), CALENDAR_FRAG_TAG);
     }

    ft.commit();
}
于 2013-07-08T19:54:48.897 回答