我有一个包含两个片段的活动。活动具有不同的纵向和横向布局,但片段保持不变(使用标签创建并重复使用)。在其中一个片段中我有xml定义的textview,它有android:freezesText =“true”行,但它仍然在屏幕旋转后丢失文本。为什么会发生这种情况?
片段的布局:
<?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:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dip10"
android:freezesText="true"
android:gravity="center"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
片段的类:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayFrag extends Fragment {
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_frag, container, false);
TextView tvDisplay = (TextView) view.findViewById(R.id.tvDisplay);
tvDisplay.setText(getArguments().getString(Keys.NAME)
+ ", how are you? your phone number is " + getArguments().getString(Keys.PHONE));
return view;
}
}
活动:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragMan = getSupportFragmentManager();
InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
if (inputFrag == null) {
fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
.commit();
} else {
fragMan.beginTransaction().replace(R.id.hook1, inputFrag, Keys.TAG_INPUT_FRAG).commit();
}
}
public void submit(View view) {
FragmentManager fragMan = getSupportFragmentManager();
DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag == null) {
displayFrag = new DisplayFrag();
displayFrag = putBundle(displayFrag);
fragMan.beginTransaction().add(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG).commit();
} else {
fragMan.beginTransaction().replace(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG)
.commit();
}
}
private DisplayFrag putBundle(DisplayFrag displayFrag) {
Bundle bundle = new Bundle();
bundle.putString(Keys.NAME, ((EditText) findViewById(R.id.etName)).getText().toString());
bundle.putString(Keys.PHONE, ((EditText) findViewById(R.id.etPhone)).getText().toString());
displayFrag.setArguments(bundle);
return displayFrag;
}
}
(提交响应另一个片段上的按钮,其行为正确)。
答案 0 :(得分:0)
找到一个解决方案:根本不需要.replace()片段。这是MainActivity和DisplayFrag的新代码:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragMan = getSupportFragmentManager();
InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
if (inputFrag == null) {
fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
.commit();
}
DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag == null) {
fragMan.beginTransaction().add(R.id.hook2, new DisplayFrag(), Keys.TAG_DISPLAY_FRAG)
.commit();
}
}
public void submit(View v) {
String name = ((EditText) findViewById(R.id.etName)).getText().toString();
String phone = ((EditText) findViewById(R.id.etPhone)).getText().toString();
if (!"".matches(name) && !"".matches(phone)) {
FragmentManager fragMan = getSupportFragmentManager();
DisplayFrag displayFrag =
(DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
if (displayFrag != null) {
displayFrag.display(name, phone);
}
}
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayFrag extends Fragment {
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_frag, container, false);
return view;
}
public void display(String name, String phone) {
TextView tvDisplay = (TextView) getActivity().findViewById(R.id.tvDisplay);
tvDisplay.setText(name + ", how are you? your phone number is " + phone);
}
}
现在接受我的回答。如果有人有任何其他答案,建议或知识可以分享,我会很乐意接受不同的答案。
答案 1 :(得分:0)
因为你的活动每次都会创建一个新片段。你可以通过检查savedInstanceState来做到这一点。希望我的榜样能让你感觉更好。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null){
fragMan.beginTransaction().add(R.id.hook1, new InputFrag())
.commit();
fragMan.beginTransaction().add(R.id.hook2, new DisplayFrag())
.commit();
}
}
并且在某些版本上发生了一些错误。
@Override protected void onSaveInstanceState(Bundle outState) {
// first saving my state, so the bundle wont be empty.
// http://code.google.com/p/android/issues/detail?id=19917
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
片段与活动的生命周期不同,设置为
android:configChanges="orientation
不会真正解决问题。
答案 2 :(得分:-1)
通过将此活动添加到您的活动/活动中,修改AndroidManifest.xml中的活动以覆盖方向更改行为:
android:configChanges="orientation|keyboardHidden"
它应该看起来像这样:
<activity android:name=".activity.LoginActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
</activity>