2012-04-05 4 views
6

같은 활동에 표시 될 두 개의 다른 단편이있는 project 테스트를 만들었습니다. 하나의 조각은 풍경 용이고 다른 하나는 초상화 용 조각입니다.오리엔테이션이 바뀌면 다른 조각이로드되지 않습니다. (안드로이드)

# My unique activity 
public class MainActivity extends FragmentActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

# First Fragment 
public class LandscapeFragment extends Fragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false); 
     v.setText("LANDSCAPE"); 
     return v; 
    } 
} 

# Other Fragment 
public class PortraitFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false); 
     v.setText("PORTRAIT"); 
     return v; 
    } 
} 

그리고 두 개의 main.xml, 하나는 layout /에 있고 다른 하나는 layout-land /에 있습니다. 각 main.xml은 사용할 올바른 Fragment를 가리 킵니다.

<!-- layout-land/main.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment" 
    android:name="br.luckcheese.test.LandscapeFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" /> 

<!-- layout/main.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment" 
    android:name="br.luckcheese.test.PortraitFragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" /> 

<!-- layout/fragment.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" /> 

Landscape에서 앱을 열면 LandscapeFragment가 표시되고 Portrait에서 열면 PortraitFragment가 표시됩니다. 여태까지는 그런대로 잘됐다.

가로로 열고 장치를 세로로 회전하면 LandscapeFragment가 다시로드되고 표시됩니다. 예상되는 동작은 아닙니다. PortraitFragment가로드되어 있어야합니다.

장치가 세로 방향으로 시작한 다음 가로 방향으로 회전하는 경우에도 똑같은 결과가 나타납니다. 즉, LandscapeFragment가로드되는 대신 PortraitFragment가 다시로드됩니다.

내가 뭘 잘못하고 있니?

답변

2

당신이하지 않는 것이 확인 : android:config="orientation" 당신의 manifest.xml 당신이 레이아웃에 문제가

+0

아니, 난 기본 매니페스트에 추가 된 독특한 라인 'android : screenOrientation = "sensor"'액티비티의 경우 – Luckcheese

+0

액티비티의 onCreate()에서 중단 점을 만들어 장치를 회전 할 때이 메서드가 호출되는지 여부를 확인 하시겠습니까? –

+0

예. 그것은이라고. 내 활동과 각 조각에 대한 onCreate에 로그 메시지를 추가했습니다. 그리고 장치를 회전 시키면 잘못된 조각의 onCreate가 활동의 onCreate 후에 호출됩니다. – Luckcheese

1

의은 조각 ID는 동일합니다. 레이아웃

fragmentportrait/main.xml에 배치 토지/main.xml에 대한

fragmentlanscape : 는 것처럼,이 단편 ID를 변경합니다. (또한 변경 : 조각의 파편 =에서 getSupportFragmentManager() findFragmentById (R.id.fragment)

frag.setRetainInstance (거짓) .)

+0

좋아, 그 노동자. 그러나 나는 왜 다른 ID를 추가 할 필요가 있는지 알지 못했습니다. 왜냐하면 내 코드에서 나는 frag.setRetainInstance (false)를 설정하도록 테스트해야하기 때문이다. 그러나 FrameLayout을 사용하여 고유 한 레이아웃을 만들고 getResource(). getConfiguration(). orientation에 의해 현재 방향을 테스트 한 후 코드로 조각을 추가하는 작업도 수행합니다. – Luckcheese

관련 문제