I have different layouts for portrait and landscape mode. layout\activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout android:id="@+id/container"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
And layout-large\activity_main.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentorientationchangetest.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentorientationchangetest.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
My fragments are same, fragment1.xml
and fragment2.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2" />
<EditText android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_below="@+id/text"/>
</RelativeLayout>
What I want is when I enter some text in edit text in my fragments and I rotate the screen, the edit text doesn't lose its input text.
Since the fragments in portrait and landscape mode are not one instance, I cannot use setRetaintInstance
or onSaveInstanceState
. How can I do that without defining two activities and without use of onConfigurationChanged
?
Note: Without adding any code, each fragment saves its data. So If I enter some text in fragment2 in portrait mode and rotate to landscape and rotate to portrait mode again, I see the entered text.