Android 为不同方向使用不同布局时,在方向更改时保存碎片状态
我有不同的纵向和横向模式布局<代码>布局\活动\u main.xml:Android 为不同方向使用不同布局时,在方向更改时保存碎片状态,android,android-activity,fragment,android-orientation,android-configchanges,Android,Android Activity,Fragment,Android Orientation,Android Configchanges,我有不同的纵向和横向模式布局布局\活动\u 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"
<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>
<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>
<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>
我的片段是相同的,fragment1.xml
和fragment2.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>
<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>
<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>
我想要的是,当我在片段中的编辑文本中输入一些文本并旋转屏幕时,编辑文本不会丢失其输入文本 由于纵向和横向模式下的片段不是一个实例,因此我不能使用
setRetainInstance
或onSaveInstanceState
。在不定义两个活动和不使用onConfigurationChanged
的情况下,如何做到这一点
注意:在不添加任何代码的情况下,每个片段保存其数据。因此,如果我在fragment2中以纵向模式输入一些文本,然后旋转到横向模式,再旋转到纵向模式,我会看到输入的文本 当我编写更多代码时,我有时会忽略一些简单的想法,如:
SharedPreferences myText = getSharedPreferences("txt", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("userText", "Your text that you want to save");
editor.commit();
并阅读:
SharedPreferences myText = context.getSharedPreferences("txt", 0);
String userInput = myText .getString("userText", "");
然后根据
userInput
设置编辑文本 我知道可以使用其他组件,如SharedReferences
或BroadCastReceiver
或对片段实例进行静态引用。但是我认为它们不是好主意,因为它们不是为这种情况而设计的。如果没有onSaveInstanceState
和setRetainInstance
您最后的选择是SharedReferences
您提到的其他组件根本不适合,而且也很危险,比如“静态引用片段实例”然而,我在主活动中使用了onSaveInstanceState
而不是片段。通过检查显示的是哪个片段,我想肯定还有另一种简便的方法可以做到这一点。“我想要的是,当我在片段中的编辑文本中输入一些文本,并旋转屏幕时,编辑文本不会丢失其输入文本。”注意:在不添加任何代码的情况下,每个片段保存其数据。因此,如果我在fragment2中以纵向模式输入一些文本,然后旋转到横向模式,再旋转到纵向模式,我会看到输入的文本。“那么问题是什么?另请注意:如果您要编辑您的问题,请在某个问题之后添加“编辑”或“更新”answered@mmlooloo我处于肖像模式。我在编辑文本中输入一些文本,然后旋转到横向。我看不到输入的文本,然后再次将屏幕旋转到纵向模式,我看到输入的文本。当我从陆地到港口和维切维萨时,我应该怎么做才能看到输入的文本?因为你的片段不是一个实例,你的问题实际上属于片段通信的范畴,我认为我的解决方案是最简单的。你可以等别人,但我的答案就是我的答案。