Preserving fragment state across orientation change when using different layouts for different orientations

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 the 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>

      

I want when I enter text into edit text in my snippets and I rotate the screen, the edit text does not lose the input text.

Since the fragments in portrait and landscape modes are not one instance, I cannot use setRetaintInstance

or onSaveInstanceState

. How can I do this without defining two actions and without using it onConfigurationChanged

?

Note. Without adding code, each chunk retains its data. So if I enter some text in snippet2 in portrait mode and turn to landscape and go back to portrait mode again, I see the text I entered.

+3


source to share


1 answer


Since I code more, I sometimes ignore a simple idea like:

SharedPreferences myText = getSharedPreferences("txt", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("userText", "Your text that you want to save");
editor.commit();

      

and reading:



SharedPreferences myText = context.getSharedPreferences("txt", 0);
String userInput = myText .getString("userText", "");

      

then set your edit text to match userInput

.

0


source







All Articles