3

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.

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • "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." and "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." **so what is the question?** and **side note: if you are going to edit your question add Edit or Update after some one answered** – mmlooloo Oct 03 '14 at 06:49
  • @mmlooloo I am in portrait mode. I enter some text in edit text, then rotate to landscape. I don't see entered text, then rotate the screen again to portrait mode and I see entered text. What should I do to see entered text when going from land to port and viceversa? – Misagh Emamverdi Oct 03 '14 at 07:16
  • because your fragments are not one instance your problem actually falls in category of fragment communication and I think my solution is the simplest one. you can wait for others but my solution is my answer. – mmlooloo Oct 03 '14 at 07:26

1 Answers1

0

As I code more I sometimes ignore 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 editText according to userInput.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • I know it is possible by using other components such as `SharedPreferences` or `BroadCastReceiver` or have static reference to fragment instances. But I think they are not good ideas because they are not made for this case. – Misagh Emamverdi Oct 03 '14 at 06:03
  • without `onSaveInstanceState` and `setRetaintInstance` your last choice is `SharedPreferences` other component that you mentioned is not suited at all and also dangerous like "have static reference to fragment instances" – mmlooloo Oct 03 '14 at 07:06
  • However I did it by using `onSaveInstanceState` in main activity not fragments by checking which fragment is showing, I was thinking there must be another handy way to do that. – Misagh Emamverdi Oct 03 '14 at 07:36