orientation change between different layouts for landscape and portrait seems to be easy, however I cannot find a solution for my current situation.
I want to use a dual-pane layout with two fragments in landscape mode
res\layout-land\dashboard_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment class="xxx.DashboardFragment"
android:id="@+id/master_frag"
android:layout_width="@dimen/timeline_size"
android:layout_height="match_parent"/>
<fragment class="xxx.TaskInstanceFragment"
android:id="@+id/details_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and a single-pane layout in portrait mode
res\layout\dashboard_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment class="xxx.DashboardFragment"
android:id="@+id/master_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
I want to note that the DashboardFragment consists of a ViewPager. Do not know if it is of importance.
Now, when I change the orientation from landscape (dual) to portrait (single) the app crashes, because it wants to update the details_frag. When I start the activity in portrait, it works, only switching from landscape to portrait makes problems.
In the activity there is the following code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
Fragment frag = getSupportFragmentManager().findFragmentById(R.id.details_frag);
mDualFragments = frag != null;
/* .. */
}
Now the FragmentManager still finds the details fragment when I switch to portrait mode and thus mDualFragments is still true but should be false, which leads to a later call to a method in the details fragment.
So, why is this? 🙂
Regards,
Konsumierer
Because
findFragmentByIdnot only returns the fragment in the current layout, but also previously displayed fragments. Try this instead: